Android Question Exit with return about 'for'

alirezahassan

Active Member
Licensed User
hi all,
i have a sub
I want to return the value of i when the condition is correct.

If I write like this, it always returns a value of -1
B4X:
Sub getidlist(us As List,user As String) As Int
    For i = 0 To us.Size-1
        Dim map As Map = us.Get(i)
        If map.Get("UserName").As(String) = user Then ' If the condition is correct, I want to exit the sub and return the value of i
            Exit  '***
            Return i  '***
        End If
    Next
    Return -1
End Sub

If I write like this, the continuation of the code will not run (Because Return) and the 'for' will run again.
B4X:
Sub getidlist(us As List,user As String) As Int
    For i = 0 To us.Size-1
        Dim map As Map = us.Get(i)
        If map.Get("UserName").As(String) = user Then 'If the condition is correct, I want to exit the sub and return the value of i
            Return i  '***
            Exit  '***
        End If
    Next
    Return -1
End Sub
 
Last edited:
Solution
First sub is ok
only remove Exit

B4X:
For i = 0 To us.Size-1
        Dim map As Map = us.Get(i)
        If map.Get("UserName").As(String) = user Then ' If the condition is correct, I want to exit the sub and return the value of i
            Return i
        End If
    Next
   
    Return -1

behnam_tr

Active Member
Licensed User
Longtime User
First sub is ok
only remove Exit

B4X:
For i = 0 To us.Size-1
        Dim map As Map = us.Get(i)
        If map.Get("UserName").As(String) = user Then ' If the condition is correct, I want to exit the sub and return the value of i
            Return i
        End If
    Next
   
    Return -1
 
Upvote 2
Solution
Top