Access Parent Instance Variables from a Nested Class

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Access Parent Instance Variables from a Nested Class

16 Feb 2018, 15:50

Hello,

Is there a recommended way for a method in a nested class to access the properties of the parent object?

For example:

Code: Select all

class ClassName 
{
    InstanceVar := Expression
    class NestedClass
    {
    	Method()
    	{
        	; How do I access InstanceVar from here?
    	}
     }
}
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Access Parent Instance Variables from a Nested Class

16 Feb 2018, 16:35

@JoeSchmoe you need a reference towards the instance of ClassName in the NestedClass.
But unless we know how your classes relate to one another it's difficult to say.
Recommends AHK Studio
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Access Parent Instance Variables from a Nested Class

16 Feb 2018, 16:56

Some ideas. Based on the example by Helgef here:
object classes: redefine __Set() temporarily / general queries - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 47#p193947

Code: Select all

q::
obj := ClassName.NestedClass.Method()

obj := new ClassName.NestedClass
obj.Method()

ClassName.__Init.Call(o:=[])
MsgBox, % o.InstanceVar
return

class ClassName 
{
    InstanceVar := "Expression"
    class NestedClass
    {
    	Method()
    	{
        	; How do I access InstanceVar from here?
		ClassName.__Init.Call(o:=[])
		MsgBox, % o.InstanceVar " " A_ThisFunc

		vClass := StrSplit(A_ThisFunc, ".")[1]
		%vClass%.__Init.Call(o:=[])
		MsgBox, % o.InstanceVar " " A_ThisFunc
    	}
     }
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Access Parent Instance Variables from a Nested Class

16 Feb 2018, 17:01

I would advise against this due to a mass of reasons that I'm not going to mention here.
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Access Parent Instance Variables from a Nested Class

17 Feb 2018, 12:20

The important point is that instantiating either of the two classes, doesn't imply an instance of the other. Hence, as hinted by nnnik, if the child needs a parent, the child needs to create it (the parent doesn't need to know :shh: ), or the parent needs to create a child and tell it (and then the parent can forget it ever happened :D ).

Cheers.
User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: Access Parent Instance Variables from a Nested Class

15 Mar 2018, 10:02

Helgef wrote:The important point is that instantiating either of the two classes, doesn't imply an instance of the other. Hence, as hinted by nnnik, if the child needs a parent, the child needs to create it (the parent doesn't need to know :shh: ), or the parent needs to create a child and tell it (and then the parent can forget it ever happened :D ).

Cheers.
Thanks, everyone, and especially Helgef for that super clear summary. It all makes perfect sense now. :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: alawsareps, Chunjee, Joey5, mebelantikjaya, mikeyww and 319 guests