Calling a class object by string name

Get help with using AutoHotkey and its commands and hotkeys
wpb
Posts: 62
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: 1177
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
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()
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 & WIN_7+ Argentina
wpb
Posts: 62
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: 4066
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: 1188
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: 4066
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”

Who is online

Users browsing this forum: AHKStudent, Bing [Bot], c7aesa7r, Chunjee, FrancoisM, hasantr and 58 guests