Is there a way to access each nested class in a class? I don't see a way to do this using the for-loop, but I'm probably wrong.
For example, maybe each nested class has a variable called "var" and I want to display the contents of it (assuming it's a string) using MsgBox.
EDIT: Helgef and teadrinker both provided a working answer, found below.
For Each Nested Class
For Each Nested Class
Last edited by GenericEntity on 18 Oct 2017, 19:01, edited 2 times in total.
Re: For Each Nested Class
Try this,
See for-loop, enumerator and _newEnum().
Cheers.
Code: Select all
; Custom enumerator:
class nestedClassenum {
__new(outerClass){
this.enum:=outerClass._NewEnum()
}
_newEnum(){
return this
}
next(byref className:="",byref classObject:=""){
local key, val
while this.enum.next(key,val) {
if isObject(val) && val.haskey("__class") {
className:=key, classObject:=val
return true
}
}
return false
}
}
; Example:
for className, classObject in new nestedClassenum(A)
(new classObject()).g(className)
class A {
static var:="hi"
class B {
var:="contents of var in A.B"
g(a){
msgbox % "hi from nested class: " a "`n" this.var
}
}
class C {
var:="contents of var in A.C"
g(a){
msgbox % "hi from nested class: " a "`n" this.var
}
}
}
Cheers.
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: For Each Nested Class
Hi, Helgef,
Isn't it too complicated?
Isn't it too complicated?
Code: Select all
for k, v in A
if v.HasKey("__Class")
(new v).g(k)
class A {
static var:="hi"
class B {
var:="contents of var in A.B"
g(a){
msgbox % "hi from nested class: " a "`n" this.var
}
}
class C {
var:="contents of var in A.C"
g(a){
msgbox % "hi from nested class: " a "`n" this.var
}
}
}
Re: For Each Nested Class
Hello teadrinker
I don't find it complicated, I find it nice to only enumerate the relevant items. It also favors code reuse, and is easy to extend to visit deeper nested classes. I leave it as an exercise to the interested reader to implement that
Cheers.
I don't find it complicated, I find it nice to only enumerate the relevant items. It also favors code reuse, and is easy to extend to visit deeper nested classes. I leave it as an exercise to the interested reader to implement that
Cheers.
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: For Each Nested Class
I understand your idea, but it seems to me, both codes do the same. Consider:I find it nice to only enumerate the relevant items
Code: Select all
; Custom enumerator:
class nestedClassenum {
__new(outerClass){
this.enum:=outerClass._NewEnum()
}
_newEnum(){
return this
}
next(byref className:="",byref classObject:=""){
local key, val
while this.enum.next(key,val) {
MsgBox, % "Enumerated item: " . key
if isObject(val) && val.haskey("__class") {
className:=key, classObject:=val
return true
}
}
return false
}
}
; Example:
for className, classObject in new nestedClassenum(A)
(new classObject()).g(className)
class A {
static var:="hi"
class B {
var:="contents of var in A.B"
g(a){
msgbox % "hi from nested class: " a "`n" this.var
}
}
class C {
var:="contents of var in A.C"
g(a){
msgbox % "hi from nested class: " a "`n" this.var
}
}
}
Code: Select all
for k, v in A {
MsgBox, % "Enumerated item: " . k
if v.HasKey("__Class")
(new v).g(k)
}
class A {
static var:="hi"
class B {
var:="contents of var in A.B"
g(a){
msgbox % "hi from nested class: " a "`n" this.var
}
}
class C {
var:="contents of var in A.C"
g(a){
msgbox % "hi from nested class: " a "`n" this.var
}
}
}
It's really easy:I leave it as an exercise to the interested reader to implement that
Code: Select all
EnumerateClasses(A)
EnumerateClasses(currentClass) {
for k, v in currentClass {
if v.HasKey("__Class")
(new v).g(k), EnumerateClasses(v)
}
}
class A {
static var:="hi"
class B {
var:="contents of var in A.B"
g(a){
msgbox % "hi from nested class: " a "`n" this.var
}
class C {
var:="contents of var in A.B.C"
g(a){
msgbox % "hi from nested class: " a "`n" this.var
}
}
}
class D {
var:="contents of var in A.D"
g(a){
msgbox % "hi from nested class: " a "`n" this.var
}
}
}
Re: For Each Nested Class
I understand the code, and as you see, I do the same thing, internally,it seems to me, both codes do the same. Consider:As you can see, in both cases "var" is enumerated.Code: Select all
;...
Code: Select all
while this.enum.next(key,val) {
if isObject(val) && val.haskey("__class")
;...
You are not grasping my point about code reuse and extendabillity it seems. The exercise (for anyone interested) is to write,I understand your idea [...] It's really easy:Code: Select all
;...
Code: Select all
class deepNestedClassEnum extends nestedClassenum {
; ???
}
- scope
- flow of control
- reusabillity
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: For Each Nested Class
Ok, I won't mind, if someone prefer your method!
Cheers.
Cheers.
Re: For Each Nested Class
That would probably come down to user preference and use case. I don't see a speed difference, as both finish in an average of 0.18-0.21 seconds.
Not sure which one to mark as "correct answer" though
Not sure which one to mark as "correct answer" though
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: For Each Nested Class
Helgef was the first, a prize to him!
Re: For Each Nested Class
You are not required to choose an answer. You can mark your own (first) post, indicating that the topic is solved, if you are statisfied. It is a short thread, and will probably remain so. Any later visitors can scan it in search for answers.
Cheers.
Cheers.
Who is online
Users browsing this forum: Google [Bot], Theda and 271 guests