Android Question Solution regarding comparison of objects in list

Discussion in 'Android Questions' started by maddy, Jul 28, 2019.

  1. maddy

    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.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.
     
  3. maddy

    maddy Member Licensed User

    hey b4a community
    Code:
    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.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    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.
     
  5. maddy

    maddy Member Licensed User

    Thanks for helping me out
     
  6. emexes

    emexes Well-Known Member Licensed User

    Just in case it got lost in translation, but the answer is:
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice