Strange thing happening on a Class

Hi there!

I have discovered some strange behavior with a class in Xojo. It’s not that easy to explain but I’ll give it a try anyway and maybe it’s not a problem with Xojo but with my code. So, this is what I have as code:

[code]j = 1
For k = 100 To j Step -1
statAll(k + 1) = statAll(k)
Next k

statAll(j).intValue = intRandomNumber[/code]
statAll is a class with one property: intValue. My expectation would be that after the code has run, statAll(1). intValue = intRandomNumber and statAll(2).intValue is what was previously in statAll(1).intValue. That’s not the case though. Xojo will assign intRandomNumber to statAll(1). intValue AND to statAll(2).intValue.

If I do this (adding .intValue), it works.

[code]j = 1
For k = 100 To j Step -1
statAll(k + 1).intValue = statAll(k).intValue
Next k

statAll(j).intValue = intRandomNumber[/code]

No idea what’s happening.

In your 1st example you replace each class by another class.
When k=1 you replace the class statAll(2) by class statAll(1).
At this point both classes have the same address (check in debug), statAll(1) and statAll(2) are the same class instance.
When you change statAll(j) (j is always 1), you change one instance that exists twice…

When you use the statAll(x).intValue, you have a value, not a class instance.

seems you have both an array and a class with the same name ?

j = 1
For k = 100 To j Step -1
  statAll(k + 1) = statAll(k)
Next k

this will make the REFERENCE stored in statAll(k+1) refer to the exact same object as statAll(k)
its not a copy
when the loop is done all the items changed in the loop refer to the same instance

[quote=492049:@Norman Palardy]seems you have both an array and a class with the same name ?

j = 1
For k = 100 To j Step -1
  statAll(k + 1) = statAll(k)
Next k

this will make the REFERENCE stored in statAll(k+1) refer to the exact same object as statAll(k)
its not a copy
when the loop is done all the items changed in the loop refer to the same instance[/quote]
Sorry, I wasn’t specific enough. statAll(1000) is a property of the class “Stat_Class” which has one property intValue.

[quote=492031:@Olivier Colard]In your 1st example you replace each class by another class.
When k=1 you replace the class statAll(2) by class statAll(1).
At this point both classes have the same address (check in debug), statAll(1) and statAll(2) are the same class instance.
When you change statAll(j) (j is always 1), you change one instance that exists twice…

When you use the statAll(x).intValue, you have a value, not a class instance.[/quote]
Yes, in debug the intValue property of both statAll(1) and statAll(2) are changed at the same line: statAll(j).intValue = intRandomNumber

I didn’t know that’s normal. I thought if you do this statAll(k + 1) = statAll(k) all that happens is that the properties of statAll(k+1) and statAll(k) are identical. As you said they are the exact same class instance. So when I have more properties than just one (intValue), I will have to assign all properties one by one.

Thanks a lot!

Yes, this works like other objects, e.g. MessageDialogButtons:

dim b1,b2 as MessageDialogButton

b1=new MessageDialogButton
b2=new MessageDialogButton

b1.caption=“1”
b2.caption=“2”

b2=b1
break //b2.caption=“1”. The “former” b2 has gone, released since there’s no more reference to it.