VB dictionary of Objects ByRef

2019-09-02 14:09发布

问题:

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?