Page 1 of 1

Calling a class object by string name

Posted: 09 Mar 2018, 16:21
by wpb
Hello,
I have a collection of objects stored. I also have a stored a list of obj.__Class strings - so in effect I have a list of the types of object in my collection.
I want to call a method of the class object (what's called the base or prototype object here: https://autohotkey.com/docs/Objects.htm#Custom_Classes), but I don't have an object variable, only a string variable containing the class name. Is that possible?

Code: Select all

class MyClass
{
   someMethod()
   {
   
   }
}

strClassName := "MyClass"
; How do I call MyClass.someMethod() using strClassName?
TIA!

Re: Calling a class object by string name

Posted: 09 Mar 2018, 16:37
by Flipeador

Code: Select all

class MyClass
{
   someMethod()
   {
       MsgBox someMethod
   }
}

strClassName := "MyClass"
; How do I call MyClass.someMethod() using strClassName?
%strClassName%.someMethod()

Re: Calling a class object by string name

Posted: 11 Mar 2018, 07:07
by wpb
Thank you very much. Couldn't find it in the documentation.

Re: Calling a class object by string name

Posted: 27 Jan 2020, 12:33
by cefay
@Flipeador Ok, how is that possible when someMethod isn't static? :shock:

Re: Calling a class object by string name

Posted: 27 Jan 2020, 13:01
by swagfag
v1 makes no distinction between static methods. v2 does.
this method here is simply a method of the object(class is basically a made up construct) MyClass

Re: Calling a class object by string name

Posted: 27 Jan 2020, 13:16
by kczx3
A great example of why I much prefer writing in v2 :)

Re: Calling a class object by string name

Posted: 27 Jan 2020, 13:34
by cefay
@swagfag I'm guessing it would fail horribly if we use some non static instance fields in this methods, am I right?

Re: Calling a class object by string name

Posted: 27 Jan 2020, 13:40
by swagfag
its more likely to fail silently but rest assured, fail it shall