Compare an object to a type?

Ricky D

Well-Known Member
Licensed User
Longtime User
If I have

B4X:
Type tFare(TableID Int, FareDate As Long, AddingNew As Boolean)
Type tShift(TableID Int, ShiftDate As Long, AddingNew As Boolean

how would I write a sub to do something like

B4X:
Sub Save(obj As Object, addingnew As Boolean
    If typeof(obj)=typeof(tFare) Then
        'add or edit the Fare
        Dim f as tFare
        f = obj
        Msgbox(f.TableID,"")
    Else If typeof(obj)=typeof(tShift) Then
        'add or edit the Shift
    End If
End Sub

Is there a way to check if an object is one of my types?

I can't find anything like typeof

regards, Ricky
 

Ricky D

Well-Known Member
Licensed User
Longtime User
made a workaround :

B4X:
Type tFare(TableID Int, FareDate As Long, AddingNew As Boolean)
Type tShift(TableID Int, ShiftDate As Long, AddingNew As Boolean

B4X:
Sub Save(obj as Object, thistype As String)
    If thistype="fare" Then
        Dim f As tFare
        If f.AddingNew Then
'add a new fare
        Else
'update existing fare
        End If
    Else If thistype="shift" Then
        dim s as tShift
        s = obj
        If s.AddingNew Then
'add a new shift
        Else
'update existing shift
        End If
    End If
End Sub

call it like this

B4X:
Dim Fare As tFare
Dim Shift As tShift

    Fare.Initialize
    Fare.TableID = 3
    Fare.FareDate = DateTime.Now
    Fare.AddingNew = True

    Save(Fare,"fare")

regards, Ricky
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
thanks Erel.

Thats simplifies it

:sign0142:

regards, Ricky
 
Upvote 0
Top