Results 1 to 3 of 3

Thread: [RESOLVED] [02/03] ArrayList Search

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397

    Resolved [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

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    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:
    1. TokenNotLoaded.Sort()
    2. ' Check to see if the token is already loaded into the array
    3. Dim idx As Integer = TokenNotLoaded.BinarySearch(sToken, New CaseInsensitiveComparer)
    4.  
    5. If idx >= 0 Then
    6.   ' Do Nothing
    7. Else
    8.   TokenNotLoaded.Add(tokenMatch.ToString)
    9. End If
    Regards,

    - Aaron.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. If idx < 0 Then
    2.     TokenNotLoaded.Add(tokenMatch.ToString())
    3. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width