Object references and re-assignment problem in .net
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 = 开发者_如何学JAVANew 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:
x("foo1") = New MyType()
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 the DataObject
class, but y
will hold a value of type MyType
when it is assigned in your code. Assigning a new value to y
will only change the value of y
, 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 the y
variable. There's no way to copy a reference to the page in the dictionary.
You would do that in the dictionary itself:
C#:
x["foo1"] = new MyType();
y = x["foo1"];
VB:
x("foo1") = New MyType()
y = x("foo1")
If you really need the "pointer to pointer" thing (because you don't have access to the dictionary), you need to add a level of indirection to your entries:
Class PointerToMyType
Public Value As MyType
End Class
Dim x As New Dictionary(Of String, PointerToMyType)
'' add value
x("foo1") = New PointerToMyType() With {.Value = New MyType()}
Dim y As PointerToMyType = x("foo1")
'' use y.Value
'' later...
y.Value = New MyType()
'' now y and x("foo1") still point to the same instance
I am not very sure if i have got exactly what you are trying to achieve but if i understand correctly, you want the new y object also to be referenced in the X collection.
If that is the case, then this should do it
Dim y As DataObject = x("foo1")
'later...
.....
y = New MyType()
//Reassign the new y object to the x collection
x("foo1") = y
.....
精彩评论