I'll start off with my question and then explain the background to the question.
Question: How can I assign a
reference to an 'object' instead of making a copy?
Background
I have a nice button type and I also add a set of these buttons to another type I call a RadioButtonSet
I have sets of functions for setting text, visibility, etc. that allow me to modify the items in the types of the button. For [a simplified] example I have:
Type tButton
text as string // button caption
AllowSounds as integer
Endtype
myButton as tButton
tButton = CreateButton("FRED")
Function CreateButton(Text as string)
gui as tButton
gui.text = text
EndFunction gui // return a new tButton object
Function SetButtonSounds(gui ref as tButton, Allowed as integer)
gui.AllowSounds = Allowed
EndFunction
That all works very well. Now I add some of these buttons into my RadioButton set like this:
type tRadioButtonSet
Value as integer // the button that is currently on -1 means none
Buts as tButton[0] // array of tButtons
Count as integer // number of buttons added
endtype
Function CreateRadioButtonSet()
gui as tRadioButtonSet
gui.Value = -1
gui.Count = 0
EndFunction gui
function AddRadioButton(gui ref as tRadioButtonSet, NewBut ref as tButton)
gui.Buts.length = gui.count // zero based
inc gui.count
gui.buts[gui.Buts.length] = newBut // seems to make a COPY of NewBut
EndFunction
ButtonSet as tRadioButtonSet
ButtonSet = CreateRadioButtonSet()
// now we add a button to the set
AddRadioButton(ButtonSet,MyButton)
AddRadioButton(ButtonSet,SomeOtherButton) // not defined in this sample code
This also works well, I can call Functions that handle the button set
Now, lets say I want to change one of the properties of the buttons, like a color or if it is allowed to make click sounds...
I can call the function for updating the content of the structure:
SetButtonSounds(MyButton,1)
What I find is that the AllowSounds value is updated correctly in MyButton, however the first button added to ButtonSet is unchanged.
This leads me to believe that when I assign the added buttons to the array of buttons in AddRadioButton with the line:
gui.buts[gui.Buts.length] = newBut // seems to make a COPY of NewBut
It is in fact creating a
copy of the tButton object that was passed in by reference. This is further supported by the fact that if I change properties of a button before adding it to the ButtonSet it maintains its value.
So my question is: how can I assign a reference to an 'object' instead of making a copy? I am hoping that there is some function/modifier/syntax to do this.
Something like:
Set gui.buts[gui.Buts.length] = newBut
I tried this:
Function ByRef(b ref as tButton)
endfunction b
gui.buts[gui.Buts.length] = byref(newBut)
But sadly the assignment still seems to make a copy.
I suspect I can do this:
ButtonSet.buts[0].AllowSound = 1
But that is only OK for Functions that don't do any side-effect work in addition to just setting a property.