简体   繁体   English

我正在尝试在VB.net中实现一个字典,其中的值可以是字符串或十进制

[英]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. Visual Studio 2012为其余的代码创建了一个模板,为所有必需的例程保留了存根。 Except for the fact that the itemType can be string or Decimal I have very happy with the normal implementation of Dictionaries. 除了itemType可以是字符串或十进制的事实外,我对Dictionary的常规实现感到非常满意。 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. 我试图仅通过调用底层实现来实现add例程,但是却收到一个错误,即String类型的值无法转换为itemType。 Even if I implement all the required routines I still don't know how to convert to the string/decimal from itemType. 即使实现了所有必需的例程,我仍然不知道如何从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 . 将其更改为简单的Class DictionaryFromExcel

One approach would be to use a Dictionary(Of String, Object) , and have your type implement IDictionary(Of String, Object) . 一种方法是使用Dictionary(Of String, Object) ,并让您的类型实现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; 另一种方法是使用Dictionary(Of String, String)保存字符串,并使用Dictionary(Of String, Decimal)保存数字。 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) . 然后,您的集合可以提供IDictionary(Of String, Object)只读实现(可以读取存储在任一词典中的内容),具有Strings属性,该属性返回实现IDictionary(Of String, String)的包装对象,并且一个属性Decimals ,该属性返回实现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. 后一种方法将使您的类型仅被硬编码以处理DecimalString ,但是访问myThing.Decimals("George")该类型的用户可以使用返回的值作为Decimal而无需进行类型转换。 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" . 后一种方法的最大缺点是,如果代码执行myThing.Decimals("Fred")=4.4d ,然后再执行myThing.Strings("Fred")="Flintstone" ,则必须决定应该怎么myThing.Decimals("Fred")=4.4d Having the latter call invalidate myThing.Decimals("Fred") would be surprising, but if it doesn't it's unclear what myThing("Fred") should return. 使后者调用使myThing.Decimals("Fred")无效会使人感到惊讶,但如果不是这样,则不清楚应返回什么myThing("Fred")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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