Get Type Name

BlitzMax Forums/BlitzMax Programming/Get Type Name

Hardcoal(Posted 2014) [#1]
How can I get a type name from within?
is it possible?


Type Test_Class

  Field TypeName$

  Method New()
    TypeName= ??? <---- what should i do here?
  End Method

End Type




Wiebo(Posted 2014) [#2]
You can use reflection, or you can use a Create(typename:String) function in your type, instead of the New() method.


GfK(Posted 2014) [#3]
If you mean that you want to pass an arbitrary value to the object, then you need to use a function (it doesn't HAVE to be called Create()) - you can call it anything:

Local myObject:Test_Class = Test_Class.makeANewObject("Poop")
DebugLog myObject.TypeName


Type Test_Class
    Field TypeName:String

    Function makeANewObject:Test_Class(txt:String)
        Local t:Test_Class = New Test_Class
        t.TypeName = txt
        Return t
    End Function
End Type

Your function can contain as many parameters as you need it to. You can also have optional parameters by declaring them with a default value:
Local myObject:Test_Class = Test_Class.makeANewObject()
DebugLog myObject.TypeName

myObject = Class.makeANewObject("Bananas")
DebugLog myObject.TypeName

Type Test_Class
    Field TypeName:String

    Function makeANewObject:Test_Class(txt:String = "Poop")
        Local t:Test_Class = New Test_Class
        t.TypeName = txt
        Return t
    End Function
End Type



Derron(Posted 2014) [#4]
You could also do:



Best way is of course setting the type name on creation (as you may do other things there to). If you are used to use Getters/Setters you would try that override-getClassName()-approach.

bye
Ron


Hardcoal(Posted 2014) [#5]
I guess i wasnt clear as i thought i am, in my question.

What im trying to get is the type name
Used by the programmer source code but automaticly,
So i wont be needing to name the type.

Type Ghost_23
 Field typeName$
   Method new()
Typename= xxx
End method()
End Type


The task xxx should automaticly set the typename field on its creation.
in this case should be set automatically to 'ghost_23'.
The idea is that i wont be needing to name it manualy.

Is it possible, reading source code attribute?


d-bug(Posted 2014) [#6]
As Wiebo said: brl.reflection can do exactly this!


Type Test_Class

  Field TypeName$

  Method New()
    TypeName= TTypeID.ForObject(Self).name() 
  End Method

End Type



Hardcoal(Posted 2014) [#7]
cool d-bug :)
Weibo said that alright but some people like me dont even know what
Reflection is.
Im still a rookie at the some Elements of blitzmax.
So your example was what I desired.

From this I can expand my knowledge of "Reflections"


Hardcoal(Posted 2014) [#8]
Is there Any way from Location xxx to Set Dada.Name as "Test_class"
Since in the above example the result will be name="Extender_Class"
I want the Extender to set the name in Method New().
Type Extender_Class

  Field Name$

  Method New()
    Name=xxx
  End Method
End Type

Type Test_Class Extends Extender_Class

End Type

Dada:Test_Class = New Test_Class




Brucey(Posted 2014) [#9]
Type Extender_Class

  Field Name$

  Method New()
    Name=GetName()
  End Method

  Method GetName:String() Abstract

End Type

Type Test_Class Extends Extender_Class

  Method GetName:String()
    Return "Test_Class"
  End Method
End Type

Dada:Test_Class = New Test_Class


<Edit> Oh, what Derron said previously... I only read the last post :-p


Hardcoal(Posted 2014) [#10]
Hey Brucey.
What my purpose was is doing it without adding on Test_Class any code.
But I guess its not possible.


d-bug(Posted 2014) [#11]
The New method is the wrong place if you don't plan a New method in every subclass of Test_Class.

Type Test_Class

	Method className:String()
		Return TTypeID.ForObject(Self).name()
	End Method

End Type


Type Extender_Class Extends Test_Class

End Type


Local base:Test_Class = New Test_Class
Local ext:Extender_Class = New Extender_Class
Print "Base = "+base.className()
Print "Extender = "+ext.className()


This one should do what you've excepted with only one single method in the base class.