Page 1 of 1
Classes and variables.
Posted: 22 Jan 2015, 07:31
by samardac
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
Re: Classes and variables.
Posted: 22 Jan 2015, 09:03
by samardac
Also is it any way to make Method of one Class set/modify Instance Variables of another Class?
Re: Classes and variables.
Posted: 22 Jan 2015, 10:31
by jethrow
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
Re: Classes and variables.
Posted: 22 Jan 2015, 10:38
by samardac
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??
Re: Classes and variables.
Posted: 22 Jan 2015, 11:05
by jethrow
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.
Re: Classes and variables.
Posted: 22 Jan 2015, 11:20
by samardac
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.
Re: Classes and variables.
Posted: 22 Jan 2015, 13:38
by samardac
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
Re: Classes and variables.
Posted: 22 Jan 2015, 14:13
by HotKeyIt
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).
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.
Re: Classes and variables.
Posted: 22 Jan 2015, 14:17
by samardac
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
Re: Classes and variables.
Posted: 22 Jan 2015, 14:38
by samardac
I tried befor you edit and it worked. Noe it looks complicated.
Re: Classes and variables.
Posted: 22 Jan 2015, 14:47
by HotKeyIt
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
Re: Classes and variables.
Posted: 22 Jan 2015, 14:54
by samardac
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.
Re: Classes and variables.
Posted: 22 Jan 2015, 15:09
by jethrow
Note that HotKeyIt's example uses an instance for sub-class B, whereas mine uses the same sub-class object:
... 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
}
}
Re: Classes and variables.
Posted: 22 Jan 2015, 20:36
by lexikos
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.
Re: Classes and variables.
Posted: 22 Jan 2015, 23:46
by samardac
lexikos,
so whot do you think, is it good idea to use thi scheme?