VB dictionary of Objects ByRef

萝らか妹 提交于 2019-12-25 01:59:46

问题


I want to create a Dim Dict As New Dictionary(Of String, Object) which would point my strings to specific COM Object classes, i.e. Dict.Add("NODES",Visum.Net.Nodes) I'd have around 20 keys in dictionary, each pointing to a different class within COM object.

Basically it works, but I'm afraid it's very heavy (dict of 20 instances of big classes) and not neccessary - I'm passing whole object to a dict, while I really need just kind of pointer (ByRef).

Is there a more clever way to do it?

PS. Why I do it? Because all objects which I'd put inside the dictionary have common methods. This way I'd be able to call i.e. : Dict("Nodes").ItemByKey(13), and then Dict("Links").ItemByKey(13), etc.

Thanks in advance Rafal


回答1:


I think you are under a misconception. You are already passing, and storing, a reference to your objects. That is the way VB.NET works. (It is also the VB6 dealt with objects, if that is your background). ByVal vs ByRef parameters with object types doesn't create copies of your objects, it just indicates if the method you called could, effectively, swap out your passed object with another.




回答2:


Have a look at HybridDictionary: From MSDN: Implements IDictionary by using a ListDictionary while the collection is small, and then switching to a Hashtable when the collection gets large.

However, there is no readily-available generic version: Is there a generic version of the HybridDictionary?



来源:https://stackoverflow.com/questions/15523950/vb-dictionary-of-objects-byref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!