Android Question Is it a bug? Or mistake of mine?

dennmtr

Member
Licensed User
Please take a look in these two pictures. List size is 3 but it throws the same error with two different methods...

B4X:
    Dim i As Int
    
    Log(RecentStoreList.Size) ' Size is 3
    
    Dim Ubound As Int = RecentStoreList.Size - 1
    
    For i = 0 To Ubound
        
        Log(GetType(RecentStoreList.Get(i))) ' Throws Size is 2
        
        Dim mpcust As Map = RecentStoreList.Get(i)
...

Same...

B4X:
    For Each mpcust As Map In RecentStoreList
        If mpcust.GetDefault("server", "") = Item.Server Then ' Throws Size is 2
            RecentStoreList.RemoveAt(RecentStoreList.IndexOf(mpcust))
        End If
    Next
 

Attachments

  • cap.JPG
    76.1 KB · Views: 196
  • cap2.JPG
    99.4 KB · Views: 199

Erel

B4X founder
Staff member
Licensed User
Longtime User
No need for this:
B4X:
Dim Ubound As Int = RecentStoreList.Size - 1
    
    For i = 0 To Ubound
The upper limit is evaluated once anyway.

You are modifying the list inside the loop so its actual size changed.

Correct code:
B4X:
Dim i As Int = 0
Do While i < RecentStoreList.Size
       Dim mpcust As Map = RecentStoreList.Get(i)
        If mpcust.GetDefault("server", "") = Item.Server Then
            RecentStoreList.RemoveAt(i)
            i = i - 1
        End If
 i = i + 1
Loop
 
Upvote 0

dennmtr

Member
Licensed User
That is the fun thing about B4X compared to other tools: you can safely assume 99.99% of the time it is going to be your own mistake and not a B4X bug

Deep inside i knew it! Always the truth is in front of you...but you cannot see it
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…