简体   繁体   中英

I am trying to implement a dictionary in VB.net where the value can be either a string or decimal

I have started with the code below

Public Class DictionaryFromExcel(Of itemType)
Implements IDictionary(Of String, itemType)

Private _Dictionary As Dictionary(Of String, itemType)
Public Sub New(ByVal FileName As String, ByVal Sheet As String, ByVal StartColumnName As String)
    MyBase.New()
    _Dictionary = New Dictionary(Of String, itemType)(System.StringComparer.OrdinalIgnoreCase)

Visual Studio 2012 created a template for the rest of the code, leaving stubs for all the required routines. Except for the fact that the itemType can be string or Decimal I have very happy with the normal implementation of Dictionaries. How do I use the underlying implementations?

I have tried to implement the add routine by just calling the underlying implementation but I get an error that value of type String cannot be converted to itemType. Even if I implement all the required routines I still don't know how to convert to the string/decimal from itemType.

_Dictionary.Add(key, value.ToString)

最好的选择是在字典上实现object属性,例如

Dim dictionary As New Dictionary(Of String,  object)

Since your type is not designed to hold generic types of data, but rather two specific types, there's no reason your type should be generic. Change it to simply Class DictionaryFromExcel .

One approach would be to use a Dictionary(Of String, Object) , and have your type implement IDictionary(Of String, Object) . Another approach would be to use a Dictionary(Of String, String) to hold the strings, and a Dictionary(Of String, Decimal) to hold the numbers; your collection could then provide a read-only implementation of IDictionary(Of String, Object) (which could read out things stored in either dictionary), have a property Strings that returns a wrapper object that implements IDictionary(Of String, String) , and a property Decimals that returns a wrapper object that implements IDictionary(Of String, Decimal) .

The former approach would be able to store any type of data in the dictionary, without restriction; that may or may not be a good thing. Callers would also have to typecast any data read from your object any time they want to use it. The latter approach would cause your type to be hard-coded just to handle Decimal and String , but a user of your type who accesses myThing.Decimals("George") could use the returned value as a Decimal without a typecast. The biggest weakness of the latter approach is that you'd have to decide what should happen if code does myThing.Decimals("Fred")=4.4d and then myThing.Strings("Fred")="Flintstone" . Having the latter call invalidate myThing.Decimals("Fred") would be surprising, but if it doesn't it's unclear what myThing("Fred") should return.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM