|
-
Apr 12th, 2006, 05:18 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [02/03] ArrayList Search
How do you search an arraylist?
The code below skips the first value of the arraylist for some reason.
TokenNotLoaded = ArrayList that i'm trying to fill
Code:
TokenNotLoaded.Sort()
''check to see if the token is already loaded into the array
Dim idx As Integer = TokenNotLoaded.BinarySearch(sToken, New CaseInsensitiveComparer)
If idx > 0 Then
Else
TokenNotLoaded.Add(tokenMatch.ToString)
End If
-
Apr 12th, 2006, 05:54 PM
#2
Re: [02/03] ArrayList Search
BinarySearch returns a Zero-based index if the item is found,
otherwise the return value is negative.
So you should change your If expression to include Zero.
VB Code:
TokenNotLoaded.Sort()
' Check to see if the token is already loaded into the array
Dim idx As Integer = TokenNotLoaded.BinarySearch(sToken, New CaseInsensitiveComparer)
If idx >= 0 Then
' Do Nothing
Else
TokenNotLoaded.Add(tokenMatch.ToString)
End If
Regards,
- Aaron.
-
Apr 12th, 2006, 06:02 PM
#3
Re: [02/03] ArrayList Search
All arrays and collections are zero-based in .NET, so zero is always the index of the first item/element. Also, having an If...Else statement with only one of the blocks used is a bit silly. Unless you actually intend to add somethong else just reverse the condition:
VB Code:
If idx < 0 Then
TokenNotLoaded.Add(tokenMatch.ToString())
End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|