B4J Question Find what type of object is in a list

Tim Chapman

Active Member
Licensed User
Longtime User
I have created 2 new Types.
I have a list of one type and another list of the other type.

I want to make a sub where a list is passed to the sub.

I want the sub to be able to figure out which list is being used by finding out which Type the list is filled with.

Is that possible?
 

teddybear

Well-Known Member
Licensed User
Yes, you can get the element of the list to determine which type it is.
B4X:
log(list.get(0) is onetype)
 
Upvote 0

behnam_tr

Active Member
Licensed User
Longtime User
also you can use


B4X:
   Type MyType (Name As String, Items(10) As Int)
Type MyType2 (Name As String, Items(10) As Int)

    
Dim a As MyType
    a.Initialize
    a.Items(2) = 123
    
    Dim b As MyType2
    b.Initialize
    b.Name = "test"
    
    
    Dim list1 As List
    list1.Initialize
    list1.Add(a)
    list1.Add(b)
    
    Log(GetType(list1.Get(0)))
    Log(GetType(list1.Get(1)))
    Log(list1.get(0) Is MyType)
    Log(list1.get(1) Is MyType)
 
Last edited:
Upvote 1

teddybear

Well-Known Member
Licensed User
I get an unknown type error for onetype. I don't see a library to fix this. What am I missing?
one type and another type are you created the new types
B4X:
Type onetype(...) 'it created by yourself
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top