Newbie question array issue

Sometimes when I am creating an array by using .add, when I add another record, the previous record changes to the same value as the new record. (I realise this is longhand example, but is simplest)


Var VertTemp as new Vertices

VertTemp.X = 50
VertTemp.Y = 50
VertTemp.Z = 50


VertTemp.X = 150
VertTemp.Y = 150
VertTemp.Z = 150


This will result in

Vertex(0) = 150,150,150
Vertex(1) = 150,150,150

What am I missing?

You are creating one instance of the Vertices class, and storing two references to it in the array.

A new instance is created when you use the New keyword. You need to make sure to use it every time you want to add a new independent instance of the class.

Var VertTemp as new Vertices ' create 1st instance
VertTemp.X = 50
VertTemp.Y = 50
VertTemp.Z = 50
Vertex.add(VertTemp) ' store reference to 1st instance in the array

VertTemp = New Vertices ' create a 2nd instance in the same local variable
VertTemp.X = 150 
VertTemp.Y = 150
VertTemp.Z = 150
Vertex.add(VertTemp) ' store reference to 2nd instance in the array
1 Like

Oh, I see. So the first VertTemp variant I created can’t have new values?
That doesn’t seem logical. Don’t I often do that with a local variable anyway? The only difference here being that I am writing it to an array.

Actually, what is more confusing is that the second one replaced the first element of the array.
Am I to understand that the original local variant is permanently cloned with the first array values?
Even after more array values were added.

Are you trying to update the original values or create a separate set of values?

I was wanting to keep changing the local variable values and then adding successive lines to the main array with each set of values.
Treating the local variable as a temporary holding place, then adding to the array.

OK, that’s what I thought. You have to create a new instance of the Vertices class each time you want to add a line, like I showed, otherwise you’re just adding references to the same line over and over again. Re-using the local variable is just a convenient way to save some typing.

If that doesn’t seem logical to you, maybe you need to study the basics of object-oriented programming first.

Even the docs have some topics:

Var VertTemp as new Vertices // creates an object somewhere in memory
VertTemp.X = 50
Vertex.add(VertTemp) // adds a reference it to an array, not a copy of the object itself

VertTemp.X = 150 // you’re still talking to the same object, so you’re changing the object that the previous array entry refers to. You need to have 2 separate objects, so changing one doesn’t change the other. You do that with New, as demonstrated by Andrew.

sorry for wasting your time, maybe skip over posts with ‘newbie’ in the title in future.

Thankyou Andrew and Tim for your help.

Personally I think what’s happening is clearer without a separate variable, and way more concise. I’d have named the class Vertice rather than Vertices, since each instance represents a singular vertice, then add a constructor to Vertice so you could just do:

Vertex.add(new Vertice(50,50,50))
Vertex.add(new Vertice(150,150,150))

You want a separate Vertice in each element – not it reads much cleaner.

VerTemp is a reference type instead of a value type, so when you alter the reference type’s value, everything that points to that will reflect the changes.

If you were to create a new variable for the Vertices object, VerTemp2, that would point to a different instance.

Edit: Or as @Bob_Grommes suggested, adding a new instance within the add method is much cleaner

Value types would behave as you were expecting.

Edit: Clearly I read the question and immediately answered, not realizing your question had been answered multiple times already :slight_smile: