Get derived object class name from its method?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Seven0528
Posts: 266
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Get derived object class name from its method?

Post by Seven0528 » 23 Jan 2023, 05:04

Code: Select all

#NoEnv
#SingleInstance Force
Class SomeClass
{
	SomeMethod()    {
		/*
		Msgbox, % this.__Class		; "SomeClass"
		Msgbox, % Clipboard:=&this	; 50077280
		Msgbox, % A_ThisFunc		; "SomeClass.SomeMethod"
		*/
		Msgbox, % how_to_get_derived_object_name ; <-- I want to get "ClassA", "ClassB" and "ClassC"
	}
}

Class_A:=new SomeClass
Class_B:=new SomeClass
Class_C:=new SomeClass
Class_A.SomeMethod()
Class_B.SomeMethod()
Class_C.SomeMethod() 
ExitApp

 This is my first question on this forum, and my English isn't good. I ask for your understanding...
I wonder how to get the name of the derived object from its method. I don't even know which search word to search for.
Is this possible? Please help me!

WookieeNo1
Posts: 1
Joined: 23 Nov 2023, 00:44

Re: Get derived object class name from its method?

Post by WookieeNo1 » 24 Nov 2023, 09:05

I'm a relative noob, but a solution I'm using is to add an InstanceName field in the __New call,

ie

Code: Select all

	__New( ... sInstanceName:="" ) {
		...
		this.InstanceName := sInstanceName
	}
and then add a class function to return the value

ie

Code: Select all

	Name(){
		return ( this.InstanceName )
	}

So, your code would be:

Code: Select all

#NoEnv
#SingleInstance Force

Class SomeClass
{
	__New( sInstanceName:="" ) {
		this.InstanceName := sInstanceName
	}

	Name() {
		return ( this.InstanceName )
	}

	SomeMethod() {
		/*
		Msgbox, % this.__Class		; "SomeClass"
		Msgbox, % Clipboard:=&this	; 50077280
		Msgbox, % A_ThisFunc		; "SomeClass.SomeMethod"
		*/

		Msgbox, % this.Name()
	}
}

Class_A := new SomeClass( "Class_A" )
Class_B := new SomeClass( "Class_B" )
Class_C := new SomeClass( "Class_C" )

Class_A.SomeMethod()
Class_B.SomeMethod()
Class_C.SomeMethod() 
ExitApp

Post Reply

Return to “Ask for Help (v1)”