Changing one property of a class changes that property in every instance

Hi I am hoping someone can enlighten me with this one because its a real head scratcher that I can get around.

I have a project that contains several classes.
cls_Job = this is a specific job
cls_Set(0) = one or more sets within a cls_job instance
cls_Item(0) this is one or more items within a cls_set instance

When I try to change a specific property of the cls_Item(0) it is changing that property in every instance of the class even if it is not part of the cls_Job class.

So if I have, for example:

Job.set(1).Item(2).ItemCode = “ABCD”

Every instance of Itemcode is changed to “ABCD”. Its not limited to the Item(0) array either its any instance of Item.ItemCode is changed in the program.

I have tried variations such as

Dim NewItem as new cls_Item
NewItem = Job.set(1).Item(2)
NewItem.ItemCode = “ABCD”
<--------- at this point every instance of cls_Item.ItemCode changes.
Job.set(1).Item(2)= NewItem

I am really scratching my head here. Any help appreciated and thanks in advance.

how did you define the property?
Shared perhaps?

I am a newbie so forgive me, Its a public property and I didn’t share it. I see that if I CNTRL-Click I can convert it to shared. So I am assuming its not.

Are you creating new instances every time you append to the item array as it sounds like they are all referencing the same object.

Yes it does sound like that doesn’t it?

I am combing back through the methods that are adding or appending the Item Objects. Is it sufficient to have something like this or to ensure they are different objects?

Dim TempItem as new cls_Item

TempItem = AnotherItem
ItemArray.append TempItem

[quote=315713:@John McGrath]Dim NewItem as new cls_Item
NewItem = Job.set(1).Item(2)
[/quote]
Kevin is right: You are creating Newitem first, but then you define it to be Newitem.set(1).Item(2). So basically all of your items are pointing to the same object while the new item itself is disregarded.
If you wanted to copy properties (which is what I guess you wanted to do with the second line), you have to copy them from the original object to the new object. Or, more verbose:

When you create a data type, like an integer:

Dim a as integer = 1

space for an integer is allocated in your computer’s RAM, and the contents of these bytes define the value of the integer.
A line like

Dim b as Integer = a

copies the value of a into the RAM reserved for B.

When you create an object, like with

Dim NewItem As new cls_item

RAM is allocated according to your classes’ needs. Technically what you have with NewItem is a pointer to that RAM area.

Lines like

Dim Neweritem As new cls_item NewerItem = NewItem
will reserve space for Neweritem, but then the second line tells NewItem to point to the reserved memory space that defines NewItem. The space reserved for newerItem never gets used.

So you should do something like

Dim NewerItem As new cls_item Neweritem.someproperty = NewItem.someProperty NewerItem.anotherproperty = Newitem.anotherproperty
and so on.

(You can of course create something like a clone method that does all that for you if you need this feature frequently)

[quote=315725:@John McGrath]Dim TempItem as new cls_Item

TempItem = AnotherItem
ItemArray.append TempItem[/quote]

ItemArray.append new cls_Item

This will create a new instance and add it to the array at the same time.

Probably unnecessary to answer this, because our posts came about the same time. Just to make sure:

[quote=315725:@John McGrath]
Dim TempItem as new cls_Item
TempItem = AnotherItem
ItemArray.append TempItem[/quote]
This would again only add AnotherItem to ItemArray as you told TempItem to point to the data of Anotheritem.

Thanks everyone for your help. I created a CloneITem function and I use that before appending

So it more or less looks like this

Dim TempItem as new cls_Item
TempItem = CloneItem(AnotherItem)
ItemArray.append TempItem

I have gone through all appends and updated it and everything is good.

CloneItem ? What’s in there ?