Maybe this is a n00bish question, but I’m used to c++’s pointers and references and this is confusing me a bit. Let’s say I have the following scenario in a VB.net application:
Dim x As New Dictionary(Of String, MyType)
'I add stuff to x
Dim y As MyObject= x("foo1")
'later...
y = New MyType()
'now y and x("foo1") don't point to the same instance anymore
The situation is this: I have a collection of objects, and during execution of the program, any of them can be fetched from the collection to be used/edited by another part of the program. Now obviously if I edit the data inside the object it is allright, but what if I want to replace the object, as I did above at line 5? I would change only the local reference (y) but not the object inside the collection! Is there a way around this? How can I take with me a “reference to the object’s reference”, instead of just a reference, so if I reassign it it will also reassign the one in the collection? I hope I’m making myself clear, unfortunately english is not my native language, to be clear: in c++ this would be easy using a pointer to a pointer, or passing a pointer to the object always by reference, so calling new or reassignment on it would change the original pointer itself)
Thanks in advance.
You cannot use pointers in VB.NET (or C#, except through unsafe code).
Instead, to change the original value in the dictionary that you’ve assigned to
y, you need to access it again through the dictionary object itself, rather than through an excised reference:The reason is that the value at the specified location in the dictionary (“foo1”) is assigned to
y, not a reference to the location itself in the dictionary. I’m not really sure why you’re using theDataObjectclass, butywill hold a value of typeMyTypewhen it is assigned in your code. Assigning a new value toywill only change the value ofy, not the original value in the dictionary. It has no knowledge of that value. Think of it essentially as if you’re pulling a copy of the value out of the dictionary and placing it in theyvariable. There’s no way to copy a reference to the page in the dictionary.