Classes and variables.

Get help with using AutoHotkey and its commands and hotkeys
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Classes and variables.

22 Jan 2015, 07:31

Hi people,
Have a question, is it way that will allow nested class B to get Instance variable from class A?
Var from class A have not to be Static. Like this:

Code: Select all

Class A{
	Var:=1
	Class B{
		Msg()
		{
			MsgBox % A.Var ;What to write here or not here, to get A.Var?
		}
	}
}
Test:= New A
Test.B.Msg()
return
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: Classes and variables.

22 Jan 2015, 09:03

Also is it any way to make Method of one Class set/modify Instance Variables of another Class?
User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: Classes and variables.

22 Jan 2015, 10:31

A sub-object by default does not know what it's parent is - you would have to code that. Keep in mind that with the code from your op, each new instance of A has the same sub-object B:

Code: Select all

Test1:= New A
Test2:= New A
MsgBox % "Test1 != Test2:`n`t" &Test1 " != " &Test2
	.	 "`n`nTest1.B = Test2.B: `n`t" &Test1.B " = " &Test2.B
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: Classes and variables.

22 Jan 2015, 10:38

Yep, but I still do not know how make one nested Class get/set Instance variables of another base Class?(using Methods)
Can you tell me is it possible? If yes how?
A sub-object by default does not know what it's parent is - you would have to code that.
How??
User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: Classes and variables.

22 Jan 2015, 11:05

I prefer to think of these all as objects with key & values, rather than Class Instance variables. Then you just need to think about what object your are accessing.

Anyways, here's an approach:

Code: Select all

Class A{
  ; Pass in Instance Variable "Number"
	__New(number) {
		this.number := number
	}
  ; Create property B to access Sub-Class __B
    B[] {
      ; Define the get behavior for property B
    	get {
          ; Set the parent value of Class __B to the
          ;  current Instance of Class A calling property B
    		this.__B.parent := this
          ; Return Class __B to imitate sub-class behavior
    		return this.__B
    	}
    }
    Class __B{
        Msg()
        {
          ; parent key-value holds reference to the 
          ;  current Instance of Class A calling property B
            MsgBox % this.parent.number
        }
    }
}

Test1:= New A(1)
Test2:= New A(2)

Test1.B.Msg()
Test2.B.Msg()
See Custom Classes.
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: Classes and variables.

22 Jan 2015, 11:20

Thank you, can you write in comments explanation what is going on every line.
For me it looks a bit comlicated, for example I never use those B[]{ get{..}}. So looks like I loose logic in the middle of the script. Or may be there is more simple way?
Thanx.
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: Classes and variables.

22 Jan 2015, 13:38

I just tried to extends class B to get acces to Var but no luck.

Code: Select all

Class A{
    Var:=1
    Class B extends A{
         Msg()
        {
            MsgBox % This.Var
        }
    }
}

Test:=new A
Test.B.Msg()
Return
HotKeyIt
Posts: 2144
Joined: 29 Sep 2013, 18:35
Contact:

Re: Classes and variables.

22 Jan 2015, 14:13

Var is saved in an instance and A.B has no reference to it.
Here B is instantiated and the base of this.B.base.base is set to our new instance (this).
Spoiler
EDIT:
To make it work we have to change base of our instance as well as base of test.B.
EDIT 2:
Hope I got it right now.
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: Classes and variables.

22 Jan 2015, 14:17

HotKeyIt,
Thank you! It looks easer than previous example, can you explain more detail what is going on here. I try to understand but looks like my knowledges is not enough.

Code: Select all

       this.B:=new A.B,this.B.base.base:=this
    
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: Classes and variables.

22 Jan 2015, 14:38

I tried befor you edit and it worked. Noe it looks complicated.
HotKeyIt
Posts: 2144
Joined: 29 Sep 2013, 18:35
Contact:

Re: Classes and variables.

22 Jan 2015, 14:47

A.B is not instantiated when new A is called which means expression (instance of A).B resolves back to A.B.
By using B extends A the base of B is set to A which resolves to Class A and not our instance of A.

I have corrected the example above to make it work properly, I hope :)
- this.B:={base:this} test.B becomes a new instance and base will be our instance (test)
- static init:=A.base:=A.B will imitate Class A extends B
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: Classes and variables.

22 Jan 2015, 14:54

Good, what I wanted to make was nest sclas in base class and that nested class have to get/set instance variables in base class. Now what you are showing is beyound my understanding. If I colud understand your first example, but this one can not at all. So looks like I'll leave this idea with nested classes.
User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: Classes and variables.

22 Jan 2015, 15:09

Note that HotKeyIt's example uses an instance for sub-class B, whereas mine uses the same sub-class object:

Code: Select all

MsgBox % &obj1.B " vs " &obj2.B
... if using a new instance for sub-class B is OK, I'd recommend going that route ... though I'd do something like this:

Code: Select all

Class A {
    Var:=1
    __New(){
        this.B:= new B(this)
    }
}
Class B { ; could be a sub-class of Class A
	__New(parent) {
		this.parent := parent
	}
	Msg(){
        MsgBox % this.parent.Var
    }
}
lexikos
Posts: 7085
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Classes and variables.

22 Jan 2015, 20:36

Class A in jethrow's latest example can be simplified to this:

Code: Select all

Class A {
    Var := 1
    B := new B(this)
}
In this case, B on the left hand side of := refers to this.B, while B on the right hand side refers to the global variable.

However, note that this creates a circular reference (this.B.parent.B.parent.B.parent...), which will prevent all involved objects from being freed.
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: Classes and variables.

22 Jan 2015, 23:46

lexikos,
so whot do you think, is it good idea to use thi scheme?

Return to “Ask For Help”

Who is online

Users browsing this forum: aldrinjohnom, bushez, Rohwedder, RubbeH and 49 guests