Is it possible to get the name of a class from within a shared method of that class? I know that Introspection.GetType() can be used on instances (objects), but what if I want to access the name of the class from within a class method?
well since all you know is “the method was called” and you may / may not actually have an instance you probably cannot use anything in introspection to tell
However you CAN still tell in a hacky way since there is always the magic “CurrentMethodName” that you can rip apart
Some sort of shorthand would be much appreciated. So I can call “Self.MySharedMethod” without knowing the name of the class. Not sure Self would be the right shorthand, just using it to illustrate the point.
Thanks for the reply, Norman. I was unaware of that constant. Unfortunately, it won’t work for me, since inside a subclass, the CurrentMethodName constant will still reference the parent class from which it inherits. I need the data type of the subclass, but I want to implement the shared method only once inside the base class.
For instance, if I have ClassCat which is subclassed from ClassAnimal, I’d like to implement a shared (class) method called MyClass() in ClassAnimal which returns “ClassCat” when ClassCat.MyClass() is invoked.
Hey, I have an idea… What if the object parameter of the Introspection.GetType() method was optional. If no instance is passed in, then the type of the current class would be returned.
[quote=26802:@Steve S]Thanks for the reply, Norman. I was unaware of that constant. Unfortunately, it won’t work for me, since inside a subclass, the CurrentMethodName constant will still reference the parent class from which it inherits. I need the data type of the subclass, but I want to implement the shared method only once inside the base class.
For instance, if I have ClassCat which is subclassed from ClassAnimal, I’d like to implement a shared (class) method called MyClass() in ClassAnimal which returns “ClassCat” when ClassCat.MyClass() is invoked.
Hey, I have an idea… What if the object parameter of the Introspection.GetType() method was optional. If no instance is passed in, then the type of the current class would be returned.
-Steve[/quote]
Curious why you need the name ?
The example illustrates a problem - since you have no instance how are you planning to distinguish which is intended ?
If I actually have to type, in my original code,
ClassCat.MyClass
that’s kind of redundant since I already type “ClassCat” just to find out I meant ClassCat ?
[quote=26811:@Norman Palardy]Curious why you need the name ?
The example illustrates a problem - since you have no instance how are you planning to distinguish which is intended ?
If I actually have to type, in my original code,
ClassCat.MyClass
that’s kind of redundant since I already type “ClassCat” just to find out I meant ClassCat ?[/quote]
My example was just a simplification. What if the shared method needed to use the class name (for whatever reason). Let’s say I have a MyClassNameMunger() shared method implemented inside ClassAnimal. If the shared method’s scope is public, I would like a call to ClassCat.MyClassNameMunger() to return the munged form of “ClassCat”.
Sure, I could pass a “ClassCat” string constant to the method, but that seems redundant to me. Why can’t the shared method somehow know something about the class through which it was invoked?
[quote=26892:@Steve S]My example was just a simplification. What if the shared method needed to use the class name (for whatever reason). Let’s say I have a MyClassNameMunger() shared method implemented inside ClassAnimal. If the shared method’s scope is public, I would like a call to ClassCat.MyClassNameMunger() to return the munged form of “ClassCat”.
[/quote]
Theoretically I suppose the compiler could bake in some info about HOW you called the method to know which access path was used so it could know that you called it through the ClassCat instead of ClassAnimal.
It doesn’t and I have my doubts that such a feature request would actually be implemented.
Class Class1
Shared Function Untitled(instance as Class1 = nil) As string
if instance is nil then instance = new Class1
dim ti as Introspection.TypeInfo = Introspection.GetType( instance )
return ti.FullName
End Function
Class CustomClass1
Inherits Class1
Shared Function Untitled() As String
return Super.Untitled( new CustomClass1 )
End Function
Window1.Open
dim s as string
s = Class1.Untitled
s = CustomClass1.Untitled
break