.NET - Set Value to, Or Reference To?

Kyle.Allbright

Active member
Joined
Apr 19, 2010
Messages
28
Programming Experience
1-3
Take my example:

i=0;
k=10;

i = k; => i is now 10
k+=1; => k is now 11, i is still 10. (Simple)

So now if we say:

myStructure = ArrayOfStructures(2)
myStructure.Value = 10;

In this instance, changing the value of myStructure ALSO changes the value in ArrayOfStructures(2)

So in the first instance we're setting i equal to value of k
In the second instance we're setting myStructure equal to a reference of ArrayOfStructures(2)

What if I wanted to set myStructure equal to the value of ArrayOfStructures(2) and any change to myStructure would not affect ArrayOfStructures(2) ?

The same happens with XmlElements - when setting a new XmlElement = Root.Item("Whatever")
 
; ? This is a VB.Net forum y'know.
In this instance, changing the value of myStructure ALSO changes the value in ArrayOfStructures(2)
No, a structure is a value type, so assignments produce a copy, not a reference. A class which is a reference type would behave like that. See Value Types and Reference Types
 
Back
Top