Value vs. Reference Variables

My heading above – Value vs. Reference Variables – I got from ChatGPT since I don’t know what I’m talking about. At any rate, here’s my question: I create a variable (QstDft) from a class I made (QAClass) in a method, set the new variable (QstDft) to some new property values and … the original class variable – in this case QstClass(0) – resets to the new properties. It then is exactly the same as my method variable, QstDft.

dim QstDft as new QAclass ’ Sets QstDft to default values

QstDft = QstClass(0)
QstDft.Question = QstClass(Item).Question
QstDft.Answer = QstClass(Item).Answer

QstClass(Item) = QstDft ’ Sets to default values

I’ve had this come up before with simpler variables. ChatGPT tells me “In Xojo, when you assign one variable to another, the behavior depends on whether the variables are value types or reference types. It sounds like you’re working with a reference type, where both variables point to the same underlying object in memory.”

Any help would be appreciated.

It might be better if you explain what you are trying to achieve.

What is QstClass ? What properties does QAClass have? You’re implying you have a Constructor method for QAClass - show us its code.

What does the Xojo doc tell you about your subject header?

With the first statement you have created a QSTDft with a new QAClass (default values)

With your second statement you destroy the previous create QAClass, with default values, and assigns QstDft to a previous crated QAClass (IE QstClass(0) with its values, not necessarly the default ones)
this assignment is a pointer, so your QstDft technically points to QstClass(0)
So with the third and fourth instruction you are setting the values of some properties in QstDft to the analogous ones from QstClass(item). Note that if these properties are primitive types you are copying the values otherwise if objects the you are copy the pointers.
Now you have in QstClass(0) these two properties with the same the values as QstClass(item)

In the last instruction you are making QstClass(item) point to QstDft, IE it points to QstClass(0)

I don’t this that this is what you want to do.

dim QstDft as new QAclass // Instantiates a new instance of QAclass

QstDft = QstClass(0) // QsDft is now a reference to QstClass(0). The new instance just created is gone/irrelevant
QstDft.Question = QstClass(Item).Question // This alters QstClass(0)
QstDft.Answer = QstClass(Item).Answer // This alters QstClass(0)

QstClass(Item) = QstDft // QstClass(Item) is now a reference to QstClass(0)

QAClass is a class I created with over 30 properties such as “Question,” “Answer,” “FontSize,” etc. It is an array and I keep the default values for each Question (it’s an anacrostic puzzle-creating software) in the array starting at index #1. I’m wanting to allow for a fast return to default values by iterating through the array and resetting the 1 through however many values back to what’s held in #0.

This is helpful. Any suggestions how I can divorce a variable from the reference array?

Thx. I’m starting to get the problem. Still don’t know how to solve. See my comment below.

Properties include Question (a string), Answer (string), fontsize (integer), X (integer, Y (integer), etc.

If I understand correctly what you’re trying to do, I think all you need to do is remove the line equating QstDft to QstClass(0), and set the new instance’s property values to those of your default at index 0 (assuming the properties are “value” types e.g. String, Integer, etc):

dim QstDft as new QAclass // Instantiates a new instance of QAclass

QstDft.Question = QstClass(0).Question // This does not alter QstClass(0)
QstDft.Answer = QstClass(0).Answer // This does not alter QstClass(0)

QstClass(Item) = QstDft

usually an object (instance of a class) is always by reference.
if you have a template object and you will change other objects to default you need a Copy Method
as example
A=B.Copy 'means Copy Method create new Object and copy all properties and return the new object
or using a reset Method

A.DefaultValues 'this method change all properties back to default

or just by object, default/init values could be set in the constructor or via own constructor method
A=new B

iterating an array ListMyClass() as MyClass

For Each x As MyClass In ListMyClass 
Next

if you need to iterate through different classes with for each,
you can assign your classes an interface to have same methods.

as example this would be all the same objects, this it not want you want.

QstClass(1) = QstClass(0) 'your default object as i understood
QstClass(2) = QstClass(0)
QstClass(3) = QstClass(0)
QstClass(4) = QstClass(0)

its more like

QstClass(1) = QstClass(0).Copy
QstClass(2) = QstClass(0).Copy
QstClass(3) = QstClass(0).Copy
QstClass(4) = QstClass(0).Copy

I’ll try the .copy idea. As is obvious, I’m not a professional programmer (retired high school English teacher) and some concepts in OOP are murky to me. This QAClass (which means Question/Answers) was my first attempt at creating a class, or subclass, or whatever it is. I just wanted a variable which could hold a bunch of properties. So anyway, I’m working my way through the concept. Starting to get a bit clearer.

Another common pattern is a copy constructor.

Class QAClass

Method Constructor( ExistingInstance as QAClass )
   oNew as New QAClass
   oNew.SomeProperty = ExistingInstance.SomeProperty
   // etc...
   return oNew
End Method

Then you can do

// SomeQA is now completely separate from ExistingQA
SomeQA = New QAClass( ExistingQA )
1 Like

which means Question/Answers

this means you have a question class and a answer class
and this question class have a propertie, a array (list) of possible answers.

without static properties there is dictionary with key,value or jsonitem.

Also commonly known as a “Clone” method, which I think is a little more accurately descriptive.

1 Like

Another way (if you don’t have too many properties to initialize) is to give your class a Constructor method that takes parameters and assigns them to the new instance’s properties there. You could then have

QstClass(Item) = new QAclass(QstClass(0).Question, QstClass(0).Answer)

I have 40 properties so it’d make for a long list.

I have always had a workaround which is a method with all those 40 properties where I just grab them one by one from QstClass(0) which is my runtime default bin and store them in the new QstClass() one at a time. It works but I just wanted to see if I could do it more elegantly and perhaps with a faster runtime. So what I’ve read here has helped me understand better and I plan to tool around with things further.

One more thing while I’ve got attention. I know how to use introspection to grab the property values but I don’t know how to store them to another variable without doing the one-by-one approach. Is it doable?

the most examples using for each but if you have an array you could use for i
with two of the same objects it should possible to read a value at one index and write in other object from same class at the same index theoretical.

No.That is the best way to proceed.

1 Like

Not necessarily. You can have the class holding both a question and an answer, and an array of objects of this class. I think it’s what’s being discussed here.