Telling what type something is

BlitzMax Forums/BlitzMax Beginners Area/Telling what type something is

Will(Posted 2005) [#1]
Hi, i'm wondering if there is a way to tell what type something is, like one universal value that is in all types that differentiate them in some way. This is for use with the Get/SetGNetInt/Float/String commands. Thanks


Cajun17(Posted 2005) [#2]
No there's not an easy way to tell what type a variable is, but if your sending them over the net shouldn't you know what you're sending?


Arcadenut(Posted 2005) [#3]
You can always add a field to the classes and set the field in the constructor such as:

Type TMyType
   Field m_type$

   Method New()
      m_type$ = "TMyType" 
   End Method

End Type

Type TMyOtherType Extends TMyType

   Method New()
      m_type$ = "TMyOtherType" 
   End Method
   
End Type




HappyCat(Posted 2005) [#4]
There's an example here of how to do it with Types. But it doesn't help for simple data types because int will cast to float and vice versa.


Rimmsy(Posted 2005) [#5]
Types are easy.
type example
field a
end type

type something
field a
end type

local a:example=new example
local b:something=new something


if example(a)<>null then print "a is of type example"
if example(b)<>null then print "b is of type example"
if something(a)<>null then print "a is of type something"
if something(b)<>null then print "b is of type something"

You're casting and checking the outcome. Floats/int/strings, I don't think you can do like this.


Will(Posted 2005) [#6]
thanks, this will be useful in some of my other projects :), but yeah i'm looking for a float/int/string type teller.

EDIT: after some thought about it, i developed a way To tell. If anyone can optimize this it would be greatly appreciated
var1:String = "blah"
var2:Float = 20
var3:Int = 20

If intfloatorstring( var1 ) = 1
	Print "it's a string"
Else If intfloatorstring( var1 ) = 2
	Print "it's a float"
Else
	Print "it's an int"
EndIf
If intfloatorstring( var2 ) = 1
	Print "it's a string"
Else If intfloatorstring( var2 ) = 2
	Print "it's a float"
Else
	Print "it's an int"
EndIf
If intfloatorstring( var3 ) = 1
	Print "it's a string"
Else If intfloatorstring( var3 ) = 2
	Print "it's a float"
Else
	Print "it's an int"
EndIf

Function intfloatorstring:Int( str:String )
	For i=33 To 45
		If Instr( str , Chr(i) )
			Return 1
		EndIf
	Next
	If Instr( str , Chr(47) )
		Return 1
	EndIf
	For i=58 To 122
		If Instr( str , Chr(i) )
			Return 1
		EndIf
	Next
	If Instr( str, "." )
		For i=0 To 9
			If Instr( str, i )
				Return 2
			EndIf
		Next
		Return 1
	EndIf
	Return 3
EndFunction



Koriolis(Posted 2005) [#7]
Just out of curiosity, can you provide an example where this is usefull? You're the second one I see asking for this, and I still can't find a legitimate use.
It's obviously usefull to determine the type of an *object* given that a reference can actually point to an instance of any derived type, but for int, float or string, what use do you plan? A variable cannot ever change from being an integer and then a float, and then a string.

Also, what if I do
Local myString1$ = "1.23"
Local myString2$ = "456"
If intfloatorstring(myString1) Then
...
EndIf

If intfloatorstring(myString2) Then
...
EndIf



Will(Posted 2005) [#8]
If you can store data as an int or float, why store it as a string? And you can always convert it to a string afterwards. My usage would be purely to use the GNet Commands. GetGNetInt, GetGNetFloat, GetGNetString, SetGNetInt, SetGNetFloat, and SetGNetString. If you are passed an object and you don't know what to store it as over the network, it would be useful to know what type it is supposed to be, or convert a string to whatever is the most efficient data type. Although my solution isn't exactly efficient its just the start to what hopefully could be an answer to telling what type something is.