Passing properties of an object WITHIN an object between functions

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
OliverK
Posts: 28
Joined: 08 Mar 2023, 11:55

Passing properties of an object WITHIN an object between functions

12 Feb 2024, 11:51

Hello.

Just a (simple?) question.. how to pass a property of a class (or the object based on the class) WITHIN a function call ?

Something like

Code: Select all

#Requires AutoHotkey v2.0

testinstance := TESTME()

testinstance.MyFunction()

Class TESTME
{
    myvar := "teststring"

    MyFunction()
    {

        this.GetsCalled(&this.myvar)
    }

    GetsCalled(&myvarref)
    {
        myvarref := "newstring"
    }
}
Won't work because of the "@this.myvar" which results in an error ..

"Error: "&" requires a variable.

(..)
▶ 014: this.GetsCalled(&this.myvar)
(..)

The program will exit."


.. and, as far as I remember, the documentation states that references are not allowed for object properties, so not really a surprise..

But often than not, I need references to set some object properties within a function to change them.

I could pass them "as values" to "GetsCalled(..)" , change them and set them explicitely with something like " this.mystr := mystringnonref " (aka function header is changed to GetsCalled(mystringnonref) ) , but if "mystringnonref " would be an object (like a Buffer for a DLL Call), this would result in a reference to the local "mystringnonref" and vanish after completing "GetsCalled(..)" , right?

Which DOES work is passing the NAME of the property which I can de-reference then via "%" enclosing.. but this seems like a hack?

Code: Select all

#Requires AutoHotkey v2.0

testinstance := TESTME()

testinstance.MyFunction()


Class TESTME
{
    myvar := "teststring"


    MyFunction()
    {
        this.GetsCalled("myvar")
    }


    GetsCalled(myvarrefname)
    {
        this.%myvarrefname% := "newstring"
    }

}
There must be a better way?
teadrinker
Posts: 4400
Joined: 29 Mar 2015, 09:41
Contact:

Re: Passing properties of an object WITHIN an object between functions

12 Feb 2024, 16:54

OliverK wrote: vanish after completing "GetsCalled(..)" , right?
Nope.

Code: Select all

testinstance := TESTME()
buf := Buffer(1, 123)
testinstance.GetsCalled(buf)
MsgBox NumGet(testinstance.myvar, 'UChar')

Class TESTME
{
    myvar := "teststring"

    GetsCalled(value) {
        this.myvar := value
    }
}
OliverK
Posts: 28
Joined: 08 Mar 2023, 11:55

Re: Passing properties of an object WITHIN an object between functions

13 Feb 2024, 01:55

Ah, well.. okay.. this works, but it's no solution to my "real" problem (.. but I didn't state my REAL problem which led me to the originating post as far as I see... so.. my fault).

In this case, you have to know already that you want to address "this.myvar".

Passing a reference as a parameter would allow me to call the SAME method with DIFFERENT properties.. which is quite handy.. and several of them if I would like to do so (passing several references as parameters).
teadrinker
Posts: 4400
Joined: 29 Mar 2015, 09:41
Contact:

Re: Passing properties of an object WITHIN an object between functions

13 Feb 2024, 02:41

Objects are always passed by reference, so there's no point in passing them via &.
OliverK
Posts: 28
Joined: 08 Mar 2023, 11:55

Re: Passing properties of an object WITHIN an object between functions

13 Feb 2024, 03:10

teadrinker wrote:
13 Feb 2024, 02:41
Objects are always passed by reference, so there's no point in passing them via &.
Yes, this would've been my assumption too regarding my experience in other languages.

But..

Code: Select all



Class TESTME2
{
    myvar := "teststring2"


    MyFunction()
    {
        this.GetsCalled(this.myvar)
        msgbox this.myvar
    }


    GetsCalled(myvarrefname)
    {
        myvarrefname := "newstring2"
    }

}

testinstance2 := TESTME2()

testinstance2.MyFunction() ; Results in Msgbox "testtring2" .. should be "newstring2"?

.. results in a messagebox with the "old" value "teststring2".

Maybe I'm just blind.
just me
Posts: 9560
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Passing properties of an object WITHIN an object between functions

13 Feb 2024, 05:14

Code: Select all

        this.GetsCalled(this.myvar)
this.myvar isn't an object reference but a property getter, so you pass the string "teststring2" to the GetsCalled() method.
On the other hand &this.myvar passes a VarRef of a temporary variable containing that string.


Code: Select all

        this.GetsCalled("myvar")
...
        this.%myvarrefname% := "newstring"
is a documented way: Dereference or name substitution and Object Literal
OliverK
Posts: 28
Joined: 08 Mar 2023, 11:55

Re: Passing properties of an object WITHIN an object between functions

13 Feb 2024, 05:42

just me wrote:
13 Feb 2024, 05:14

Code: Select all

        this.GetsCalled(this.myvar)
this.myvar isn't an object reference but a property getter, so you pass the string "teststring2" to the GetsCalled() method.
On the other hand &this.myvar passes a VarRef of a temporary variable containing that string.
Well, @this.myvar was the first thing I tried, but it results in
Error: "&" requires a variable.

000: }
036: {
▶ 037: this.GetsCalled(&this.myvar)
038: msgbox(this.myvar)
039: }

Code: Select all

Class TESTME2
{
    myvar := "teststring2"


    MyFunction()
    {
        this.GetsCalled(&this.myvar)
        msgbox this.myvar
    }


    GetsCalled(&myvarrefname)
    {
        myvarrefname := "newstring2"
    }

}

just me
Posts: 9560
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Passing properties of an object WITHIN an object between functions

13 Feb 2024, 05:48

On the other hand &this.myvar would pass a VarRef of a temporary variable containing that string, if permitted. ;)
OliverK
Posts: 28
Joined: 08 Mar 2023, 11:55

Re: Passing properties of an object WITHIN an object between functions

13 Feb 2024, 08:40

So... no idea at the moment how to properly pass object properties via references in a method's parameters?
lexikos
Posts: 9688
Joined: 30 Sep 2013, 04:07
Contact:

Re: Passing properties of an object WITHIN an object between functions

01 Mar 2024, 02:33

It is not possible to construct a VarRef for a property of an object (such as foo.bar), A_Clipboard or any other built-in variable, so those cannot be passed ByRef.
Source: Functions - Definition & Usage | AutoHotkey v2
This is because a property is not a variable, and VarRef doesn't support the level of abstraction needed to support properties. A variable has a single unique location in memory. To set a property, it is necessary to have a reference to the object and the name of the property, or a reference to the object and a reference to the property's setter function (but if it's implemented with __Set, you'd still need the property name).
OliverK
Posts: 28
Joined: 08 Mar 2023, 11:55

Re: Passing properties of an object WITHIN an object between functions

01 Mar 2024, 05:16

lexikos wrote:
01 Mar 2024, 02:33
It is not possible to construct a VarRef for a property of an object (such as foo.bar), A_Clipboard or any other built-in variable, so those cannot be passed ByRef.
Source: Functions - Definition & Usage | AutoHotkey v2
This is because a property is not a variable, and VarRef doesn't support the level of abstraction needed to support properties. A variable has a single unique location in memory. To set a property, it is necessary to have a reference to the object and the name of the property, or a reference to the object and a reference to the property's setter function (but if it's implemented with __Set, you'd still need the property name).

Thank you for your answer!

So, in other words.. my "hack" is the only way?
lexikos
Posts: 9688
Joined: 30 Sep 2013, 04:07
Contact:

Re: Passing properties of an object WITHIN an object between functions

02 Mar 2024, 20:46

No, there are always other hacks.

For instance, if the property corresponds to an actual variable, you can use a reference to that variable instead of a reference to the property. I demonstrated this in the topic Byref Array elements (Wish List). (You still can't use the syntax &this.myvar.)
OliverK
Posts: 28
Joined: 08 Mar 2023, 11:55

Re: Passing properties of an object WITHIN an object between functions

04 Mar 2024, 05:47

lexikos wrote:
02 Mar 2024, 20:46
No, there are always other hacks.

For instance, if the property corresponds to an actual variable, you can use a reference to that variable instead of a reference to the property. I demonstrated this in the topic Byref Array elements (Wish List). (You still can't use the syntax &this.myvar.)
Interesting.. thanks!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Draken, shipaddicted, songdg and 29 guests