Create nested class instance from variable Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Create nested class instance from variable

02 Dec 2020, 06:55

Hello everybody

I want to create an instance of a nested class. The class name is given through a variable.

Code: Select all

class a {}
o1 := a.new()    ; Works
cn := "a"
o2 :=  %cn%.new() ; Works
class b {
	class c {}
}
o3 := b.c.new() ; Works
cn := "b.c"
o4 :=  %cn%.new() ; !!!! Fails! "Variable name contains an illegal character" 
ExitApp
Generation of o4 fails as there is a dot in my variable name. How can I generate an instance of a nested class from a variable containing a dot?

Using Autohotkey V2.0.a122

Thanks in advance
hoppfrosch
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Create nested class instance from variable  Topic is solved

02 Dec 2020, 07:32

u have to parse the string and iterate through the classes until u get to the one u wish to new up

Code: Select all

#Requires AutoHotkey v2.0-a122-f595abc2

class b {
	class c {}
}

cn := "b.c"
o4 :=  newFromString(cn)

newFromString(str, Args*) {
	ClassNames := StrSplit(str, '.')
	C := %ClassNames.RemoveAt(1)%

	for name in ClassNames
		C := C.%name%

	return C.New(Args*)
}
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: Create nested class instance from variable

03 Dec 2020, 00:22

:thumbup: Great-works exactly the way I desired.

Just to understand it:

Code: Select all

class a{ }
C := a
Obviously classnames ("a") cannot be used as variable name anymore. Haven't noticed this yet.
Where can I find documentation about this? Don*t have even an idea where to search for ... :crazy:
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Create nested class instance from variable

03 Dec 2020, 04:58

C is just a variable name i chose cause i couldnt come up with anything better. u should probably rename it to something more meaningful. it doesnt refer to the class class c {}(in ur code).
Where can I find documentation about this? Don*t have even an idea where to search for ...
u cant search for it, specifically. ud have to read the whole docs and make mental notes of where things are. or track the changelog/commit messages.
Objects->Classes wrote:When the script is loaded, this constructs a Class object and stores it in a super-global constant (read-only variable) with the name ClassName.
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: Create nested class instance from variable

03 Dec 2020, 07:36

As a non-english native you have to read this, make mental notes AND understand all the intrisic details of those sentences :lol:
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Re: Create nested class instance from variable

11 Dec 2020, 10:05

No, you can't evaluate any code using %%. i.e. dot operator. Only variable names.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: arcylix, qcgreywolf, Sem552, zabbn and 39 guests