Sub Process_Globals
Type t1(memberX As String)
Type t2(memberY As String)
End Sub
Sub AppStart (Args() As String)
Dim vt1 As t1
Dim vt2 As t2
LogType(vt1)
LogType(vt2)
End Sub
Sub LogType(o As Object)
If o Is t1 Then
Log("t1")
else if o Is t2 Then
Log("t2")
End If
End Sub
But is it possible to do the same with "Select case" instead of "If Then else"?
i think the compiler can not understand, u must make a feature request.
maybe its better to work with classes that each of them have methods for debugging.
a.MyLog
is better than MyLog(a)
because after dot you get a pick list of available methods.
How about this as a solution (bit hacky but simple)
B4X:
Sub Process_Globals
Type t1(memberX As String)
Type t2(memberY As String)
End Sub
Sub AppStart (Args() As String)
Dim vt1 As t1
Dim vt2 As t2
xLogType(vt1)
xLogType(vt2)
'StartMessageLoop
End Sub
Sub xLogType(o As Object)
Select GetType(o)
Case isType("t1")
Log("its t1")
Case isType("t2")
Log("its t2")
Case Else
Log("unknown")
End Select
End Sub
Sub isType(s As String) As String ' the name of the class of type t1 t2 etc MUST be in this module in globals
Dim ret As String = Me ' get this class name
Return ret.Replace("class ","")&"$_"&s ' add the type class name eg b4j.example.main$_typeclass
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub Process_Globals
Type T1(memberX As String)
Type T2(memberY As String)
Dim T1Type As String
Dim T2Type As String
End Sub
Sub AppStart (Args() As String)
Dim T1Ref As T1
Dim T2Ref As T2
T1Ref.Initialize
T2Ref.Initialize
T1Type = GetType(T1Ref)
T2Type = GetType(T2Ref)
Dim vt1 As T1
Dim vt2 As T2
xLogType(vt1)
xLogType(vt2)
End Sub
Sub xLogType(o As Object)
Select GetType (O)
Case T1Type
Log("Is T1")
Case T2Type
Log("Is T2")
Case Else
Log ("Unknown")
End Select
End Sub
As regard the alternate solutions, for a few types its fine, but if there were 50+ types, you would add lots of extra lines to the code.
(that came out sounding wrong, I mean for a few types then I like your methods)
I think the select - case part of your suggestions is pretty readable, but the cost is the extra code that make the complete code less readable, and it takes a lot more time to code. Then it is still better with:
B4X:
If o Is t1 Then
Log("t1")
else if o Is t2 Then
Log("t2")
End If
- it is faster to code and still more readable than your suggestions.
Your post arrives while I'm writing (or slowly spelling my english). Your creative suggestions proves to me that a language support for typetesting in select - case blocks would be well received.