I have a class called BezSeg that has various properties including node (an integer).
The following code is in the Open event of a window. That window contains a control named cnvDrawArea which is an instance of a custom control. That custom control has a property aBS which is an array of BezSeg. When this code begins, this array has no members.
Var someBezSeg As New BezSeg
someBezSeg.node = 5
// … other properties also assigned values
Self.cnvDrawArea.aBS.Add(someBezSeg)
someBezSeg.node = 20
// … other properties also assigned values
Self.cnvDrawArea.aBS.Add(someBezSeg)
At this point, there are two members of the aBS array. They both have a node property with the value 20. Even though the line of code Self.cnvDrawArea.aBS.Add(someBezSeg) = 5
was issued and briefly the first element of the aBS array had a node property of 5, eventually, that element ends up with the same property values as the second element (i.e. 20). Seemingly, as long as someBezSegment is in scope, the first element of the aBS array will simply track the value currently in someBezSeg.
If True Then
Var someBezSeg As New BezSeg
someBezSeg.node = 5
// … other properties also assigned values
Self.cnvDrawArea.aBS.Add(someBezSeg)
End If
If True Then
Var someBezSeg As New BezSeg
someBezSeg.node = 20
// … other properties also assigned values
Self.cnvDrawArea.aBS.Add(someBezSeg)
End If
This code “solves” the problem. The first element of aBS’s property node retains the value that was specifically assigned to it (5).
In this version, the variable someBezSeg goes out of scope once the code leaves the If/End If wrapping, and the first element of aBS securely holds on to its 5 value. It will not be changed by any code downstream.
I find this behavior confusing. Can someone explain it? Is there a more elegant way of addressing it than using the If/End If wrapping.
I brought someBezSeg into the code in the first place to make it easier to follow what was going on in the debugger where it is easiest to follow a local variable. The actual code, from which I have extracted this “simplified” example, is more complex.
You’re storing two references to the same instance of BezSeg. Instead create a new instance each time you add to the array.
Var someBezSeg As New BezSeg ' first instance
someBezSeg.node = 5
Self.cnvDrawArea.aBS.Add(someBezSeg) ' store the first instance in the array
someBezSeg = New BezSeg ' create a second instance in the same local variable
someBezSeg.node = 20
Self.cnvDrawArea.aBS.Add(someBezSeg) ' store the second instance
So as I understand what you are saying since the New word does not appear in any of the lines immediately above, I am only creating references to the single instance that was created with the line
Var someBezSeg As New BezSeg
However, if I wrote the line
Var AnotherBezSeg As New BezSeg = SomeArray(0)
What would I have done? Would I have created another instance that happens to have the same properties as the SomeArray(0)?
Sorry for all this verbiage, but I am trying to fix these notions in my brain.