dim s as string = "123"
dim w as wstring * 16 = "456"
dim z as wstring * 16 = "789"
dim r as tagrect
#if typeof(s) = string
MessageBox 0, "String", "test", MB_OK
#endif
#if typeof(w) = wstring
MessageBox 0, "Wstring", "test", MB_OK
#endif
#if typeof(z) = zstring
MessageBox 0, "Zstring", "test", MB_OK
#endif
#if typeof(r) = tagrect
MessageBox 0, "Rect", "test", MB_OK
#endif
So "STRING" is recognized (like all other numeric and user defined data types btw.), but why do "WSTRING" and "ZSTRING" fail? Is there another method of identifying a variable´s type from it´s name (identifier) at run time or compile time ?
You'll see that the type of e.g. a "zstring*5" is "ZSTRING * 5" (and not "ZSTRING"). You can compare the type by using typeof on both sides, but I can't think of a solution for matching any length of ZStrings.
I can use "#print TypeOf(any data type)", but is there a way to get then name of the corresponding data type (e.g "STRING", "LONG" etc.) into a string at runtime? Something like: "mystring = TypeOf(x)" (which doesn´t compile), where "x" is a "LONG" for instance.
TypeOf() is a compiler intrinsic that can be used only at compilation time (replacing itself with the type of the variable passed to it).
It cannot be used at execution time.
The only variables whose dynamic type could be retrieved (but with great difficulty if the list of possible types is unknown at compile time) during execution are only instances of user objects being defined as directly or indirectly derived from the built-in type OBJECT, because only the built-in type OBJECT provides Run-Time Type Information (RTTI).
But the list of possible static types being known at compile time, the problem to retrieve these only static types as strings at run time from the variable names is very easy to code.
See the examples in my following posts.
Last edited by fxm on May 02, 2018 8:14, edited 8 times in total.
Is there another way to get then name of the corresponding data type (not only the standard types, but also user defined types) into a string at run time? Or maybe somehow set the value of a string variable at compile time with the corresponding data type, so it is available at run time.
In other words, how to get the name of an arbitrary variable´s data type into a string variable (any of these will do: STRING, ZSTRING, WSTRING, or any pointer to the string data), which can be accessed at run time. Obviously the variable´s name and data type is known at compile time.
It isn´t a "must have" for me, but it would be a "nice to have". So maybe someone still has yet another idea for achieving, what i want somehow or other...
From the list of possible static types known at compile time, an example to retrieve these only static types as strings at run time from the variable names:
Function staticTypeName overload (Byref i As Integer) As String
Return "Integer"
End Function
Function staticTypeName overload (Byref d As Double) As String
Return "Double"
End Function
Function staticTypeName overload (Byref s As String) As String
Return "String"
End Function
'.....
'.....
'--------------------------------------------------------
Dim As Double d0
Print staticTypeName(d0)
Sleep
Type simpleUDT
Dim As Longint l
End Type
Type UDT Extends Object
End Type
Type derivedUDT Extends UDT
End Type
Function staticTypeName overload (Byref i As Integer) As String
Return "Integer"
End Function
Function staticTypeName overload (Byref d As Double) As String
Return "Double"
End Function
Function staticTypeName overload (Byref s As String) As String
Return "String"
End Function
Function staticTypeName overload (Byref su0 As simpleUDT) As String
Return "simpleUDT"
End Function
Function staticTypeName overload (Byref o As Object) As String
Return "Object"
End Function
Function staticTypeName overload (Byref u As UDT) As String
Return "UDT"
End Function
Function staticTypeName overload (Byref du As derivedUDT) As String
Return "derivedUDT"
End Function
Function dynamicTypeName overload (Byref o As Object) As String
If o Is derivedUDT Then Return "derivedUDT"
If o Is UDT Then Return "UDT"
Return "Object"
End Function
'.....
'.....
'--------------------------------------------------------
Dim As Integer i0 '' Integer type variable (only static type)
Dim As Double d0 '' Double type variable (only static type)
Dim As String s0 '' String type variable (only static type)
Dim As simpleUDT su0 '' simpleUDT type variable (only static type)
Dim As Object o0 '' Object type variable (only static type)
Dim As UDT u0 '' UDT type variable (only static type)
Dim As derivedUDT du0 '' derivedUDT type variable (only static type)
Dim Byref As Object roo0 = o0 '' Object type reference, referring to an Object instance (static type + dynamic type)
Dim Byref As Object rou0 = u0 '' Object type reference, referring to a UDT instance (static type + dynamic type)
Dim Byref As Object rodu0 = du0 '' Object type reference, referring to a derivedUDT instance (static type + dynamic type)
Dim Byref As UDT ruu0 = u0 '' UDT type reference, referring to a UDT instance (static type + dynamic type)
Dim Byref As UDT rudu0 = du0 '' UDT type reference, referring to a derivedUDT instance (static type + dynamic type)
Print , "static-type", "dynamic-type"
Print "----------------------------------------"
Print "io", staticTypeName(i0)
Print "d0", staticTypeName(d0)
Print "s0", staticTypeName(s0)
Print "su0", staticTypeName(su0)
Print "o0", staticTypeName(o0), dynamicTypeName(o0)
Print "u0", staticTypeName(u0), dynamicTypeName(u0)
Print "roo0", staticTypeName(roo0), dynamicTypeName(roo0)
Print "rou0", staticTypeName(rou0), dynamicTypeName(rou0)
Print "rodu0", staticTypeName(rodu0), dynamicTypeName(rodu0)
Print "ruu0", staticTypeName(ruu0), dynamicTypeName(ruu0)
Print "rudu0", staticTypeName(rudu0), dynamicTypeName(rudu0)
Sleep