B4J Question Is there an internal index for For Each?

Bruce Axtens

Active Member
Licensed User
Longtime User
Given this code
B4X:
For Each p  As Proxy In Main.proxyList
  If p.ip = p Then
    p.efficacy = p.efficacy + 1
    p.inuse = False
    Exit
  End If
Next
It would seem pretty hard to update the Main.proxyList List if one were using
B4X:
Main.proxyList.Set(pos,p)
as there's no way of knowing what `pos` might be. If I were to have a counter variable could I be sure that For Each was iterating from top to bottom? Is there an alternative or should I, in cases like this be doing
B4X:
For pos = 0 To Main.proxyList.Size -1
  proxyRec = Main.proxyList.Get(pos)
  If proxyRec.ip = ip Then
    proxyRec.efficacy = proxyRec.efficacy + 1
    proxyRec.inuse = False
    Main.proxyList.Set(pos,proxyRec)
    found = True
    Exit 
  End If
Next
?
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
May be instead of using a list or in this case 2 lists, you could pair the proxy as key and the pos as value in a map.

B4X:
For each p as proxy in proxies.keys 
Dim pos as int = Proxies.get(p) 
Next

Something like that.
 
Upvote 0

Bruce Axtens

Active Member
Licensed User
Longtime User
Interesting thought that. Thanks.
 
Upvote 0
Top