Calling a class object by string name

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wpb
Posts: 150
Joined: 14 Dec 2015, 01:53

Calling a class object by string name

09 Mar 2018, 16:21

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!
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Calling a class object by string name

09 Mar 2018, 16:37

Code: Select all

class MyClass
{
   someMethod()
   {
       MsgBox someMethod
   }
}

strClassName := "MyClass"
; How do I call MyClass.someMethod() using strClassName?
%strClassName%.someMethod()
wpb
Posts: 150
Joined: 14 Dec 2015, 01:53

Re: Calling a class object by string name

11 Mar 2018, 07:07

Thank you very much. Couldn't find it in the documentation.
cefay
Posts: 56
Joined: 26 Jan 2020, 12:53

Re: Calling a class object by string name

27 Jan 2020, 12:33

@Flipeador Ok, how is that possible when someMethod isn't static? :shock:
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Calling a class object by string name

27 Jan 2020, 13:01

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
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Calling a class object by string name

27 Jan 2020, 13:16

A great example of why I much prefer writing in v2 :)
cefay
Posts: 56
Joined: 26 Jan 2020, 12:53

Re: Calling a class object by string name

27 Jan 2020, 13:34

@swagfag I'm guessing it would fail horribly if we use some non static instance fields in this methods, am I right?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Calling a class object by string name

27 Jan 2020, 13:40

its more likely to fail silently but rest assured, fail it shall

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: joedf, scriptor2016, supplementfacts and 141 guests