Android Question Solution regarding comparison of objects in list

maddy

Member
Licensed User
hey b4a community
'Type area(Areaname As String,Areanumber As Int)
dim l1 as list
Dim a As area
a.Initialize
l1.Initialize
l1.Add(a=add_area("Dnkl",90))
l1.Add(add_area("NMJ",89))
l1.Add(add_area("ASSS",900))


Log( l1.IndexOf(a))
I had created a function above and when i find indexof the area(custom type) i got index of -1
but when i find index using areaname only i got the proper index.So what the solution of finding proper index through the reference of object.
 

maddy

Member
Licensed User
hey b4a community
B4X:
   Dim a As area
   a.Initialize
    l1.Initialize
    l1.Add(a=add_area("Dnkl",90))
    l1.Add(add_area("NMJ",89))
    l1.Add(add_area("ASSS",900))
     

    Log( l1.IndexOf(a))
I had created a function above and when i find indexof the area(custom type) i got index of -1
but when i find index using areaname only i got the proper index.So what the solution of finding proper index through the reference of object.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
l1.Add(a=add_area("Dnkl",90))
This syntax is invalid.

This code will not work as the types are compared for reference equality. This means that two different type instances with the same values are considered different items.

You can use a Map that maps between the area id to the actual area item.
 
Upvote 0
Top