高级哈希表? [英] Advanced Hashtable ?

查看:93
本文介绍了高级哈希表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有没有办法在VB.NET中使用哈希表来增加

特定类型的值并检索相同的值而不用不得不把它投到

原始类型?


谢谢

Marty

Hi,

Is there a way when using a hashtable in VB.NET to add value of a
specific type and retrieve that same value without having to cast it to
the original type?

Thanks
Marty

推荐答案

是的,您可以从Dictionary派生并覆盖您想要使用的方法,执行演员表。这是来自MSDN,并显示了使用

a字符串类型而不是通用对象的示例。

导入系统

Imports System.Collections


公共类ShortStringDictionary

继承DictionaryBase


默认公共属性项(键为[String])As [String]

获取

返回CType(字典(键),[字符串])

结束获取

设置

字典(键)=值

结束集

结束物业


Public ReadOnly Property Keys()作为ICollection

获取

返回Dictionary.Keys

结束获取

结束财产

Public ReadOnly属性值()作为ICollection

获取

返回Dictionary.Values

结束获取

结束财产


公共子添加(键为[String],值为[String])

Dictionary.Add(键,值)

End Sub''添加


公共功能包含ins(键为[String])As Boolean

返回Dictionary.Contains(键)

结束函数''包含


Public Sub Remove(键As [String])

Dictionary.Remove(key)

End Sub''删除


结束上课


" Marty" < XM ****** @ hotmail.com>在消息中写道

新闻:H3jne.28711
Yes, you can derive from Dictionary and the override the methods you want to
use, performing the cast. This is from MSDN, and shows an example of using
a string type instead of generic object.
Imports System
Imports System.Collections

Public Class ShortStringDictionary
Inherits DictionaryBase

Default Public Property Item(key As [String]) As [String]
Get
Return CType(Dictionary(key), [String])
End Get
Set
Dictionary(key) = value
End Set
End Property

Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property

Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property

Public Sub Add(key As [String], value As [String])
Dictionary.Add(key, value)
End Sub ''Add

Public Function Contains(key As [String]) As Boolean
Return Dictionary.Contains(key)
End Function ''Contains

Public Sub Remove(key As [String])
Dictionary.Remove(key)
End Sub ''Remove

End Class

"Marty" <xm******@hotmail.com> wrote in message
news:H3jne.28711


on1.401@clgrps13 ...
on1.401@clgrps13...

<在VB.NET中使用哈希表来添加特定
类型的值并检索相同的值而不必将其转换为
原始类型时,有没有办法?

谢谢
Marty
Hi,

Is there a way when using a hashtable in VB.NET to add value of a specific
type and retrieve that same value without having to cast it to the
original type?

Thanks
Marty



嗨罗宾,


感谢您提供的信息。 MSDN是否有关于使用此算法的性能

的详细信息?因为它仍然需要在

返回值之前进行演员。


谢谢,

Marty


Robin Tucker写道:
Hi Robin,

Thanks for the information. Does MSDN has details about the performance
of using this algorithm? Because it still has to do a cast before
returning the value.

Thank you,
Marty

Robin Tucker wrote:
是的,您可以从Dictionary派生并覆盖您想要使用的方法,执行演员表。这是来自MSDN,并显示了使用
字符串类型而不是通用对象的示例。

导入系统
导入System.Collections

公开类ShortStringDictionary
继承DictionaryBase

默认公共属性项(键为[String])As [String]
获取
返回CType(字典(键),[String ])
结束获取
设置
字典(键)=值
结束集
结束属性

Public ReadOnly属性键()As ICollection
获取
返回Dictionary.Keys
结束获取
结束财产

Public ReadOnly属性值()作为ICollection
获取
返回Dictionary.Values
结束获取
结束属性

公共子添加(键为[String],值为[String])
Dictionary.Add(键,值)
结束子''添加

公共功能包含(键As [Str ing])作为布尔值
返回Dictionary.Contains(键)
结束函数''包含

公共子删除(键为[String])
Dictionary.Remove (关键)
End Sub''删除

结束课

Marty < XM ****** @ hotmail.com>在消息中写道
新闻:H3jne.28711
Yes, you can derive from Dictionary and the override the methods you want to
use, performing the cast. This is from MSDN, and shows an example of using
a string type instead of generic object.
Imports System
Imports System.Collections

Public Class ShortStringDictionary
Inherits DictionaryBase

Default Public Property Item(key As [String]) As [String]
Get
Return CType(Dictionary(key), [String])
End Get
Set
Dictionary(key) = value
End Set
End Property

Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property

Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property

Public Sub Add(key As [String], value As [String])
Dictionary.Add(key, value)
End Sub ''Add

Public Function Contains(key As [String]) As Boolean
Return Dictionary.Contains(key)
End Function ''Contains

Public Sub Remove(key As [String])
Dictionary.Remove(key)
End Sub ''Remove

End Class

"Marty" <xm******@hotmail.com> wrote in message
news:H3jne.28711


这篇关于高级哈希表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆