Link to home
Start Free TrialLog in
Avatar of indy500fan
indy500fan

asked on

Looking for a better way of searching an ArrayList

Friends,

Right now, I have a TCP Feed that is spitting out records.  Now, one of the records contains entrant information.  I am taking out selected entrant info and putting it into an arraylist.

Seems simple enough.  Now, the entrant information is repeated every so often, and if I already have it in my arraylist, I don't need to re-add it.  So, right now, here is my code to see if the information exists in the array.  If not, it adds it.  My question is, is this the best way (going through the list to see if a match is found), or is there a better way?

Here is where the ArrayLists are defined:

    Dim DriverEntrantAL As New ArrayList
    Dim oDriverEntrant As DriverEntrant

This is where the DriverEntrant Property List is defined:

Public Class DriverEntrant
    Public CarNumber As String
    Public FirstName As String
    Public LastName As String
End Class

Here is where I look to see if it is in the arraylist (and if not I add it.):

Public Sub ProcessValidString(ByVal AMBMLPValidString As String) 'Handles RecieveValidAMBMLPString.ValidAMBMLPString

        Dim SplitUpString() As String
        SplitUpString = Split(AMBMLPValidString, "¦")

        If SplitUpString.GetUpperBound(0) = 0 Then
            Exit Sub
        End If

        Dim Record As String
        Record = SplitUpString(0)


        Select Case Record

            Case "E"

                'If CarNumber doesn't exist in array, add (3) values to array as record
                e_CarNumber = AMBParser_Entrant.GetValue(AMBMLPValidString, 4) 'Just know that this returns the CarNumber of this particular record...
                Dim DriverEntrantCarNumberTemp As String = Nothing

                For Each oDriverEntrant In DriverEntrantAL

                    DriverEntrantCarNumberTemp = oDriverEntrant.CarNumber
                    If e_CarNumber = DriverEntrantCarNumberTemp Then
                        CarNumberExistsInList = True
                        Exit For
                    Else
                        CarNumberExistsInList = False
                    End If
                Next

                If CarNumberExistsInList = False Then

                    e_Driver_FirstName = AMBParser_Entrant.GetValue(AMBMLPValidString, 61)
                    e_Driver_LastName = AMBParser_Entrant.GetValue(AMBMLPValidString, 62)

                    Dim DriverEntrantProperty As New DriverEntrant

                    DriverEntrantProperty.CarNumber = e_CarNumber
                    DriverEntrantProperty.FirstName = e_Driver_FirstName
                    DriverEntrantProperty.LastName = e_Driver_LastName
                    DriverEntrantAL.Add(DriverEntrantProperty)
                End If


Thanks in advance!

Regards,
Eric
SOLUTION
Avatar of ZeonFlash
ZeonFlash

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of indy500fan
indy500fan

ASKER

Zeon and Fernando,

Hey, I didn't think of that!  That's why I love this forum.  I'm going to try both of your solutions, and then I'll assign points.

Thank you!
Hmm...In thinking about this, there is a possiblity that two drivers will have the same car number.  For example, an experienced driver might get in the car to "shake it down" for a rookie.  In this instance, you would have two drivers with the same car number, or key.  

Any other suggestions?

Thanks,
Eric
Hi Eric;

How about using the e_CarNumber & "-" & e_Driver_LastName as the key?

Fernando
Fernando,

That concept would work, but unfortunately, I'll have to do e_CarNumber & "-" & e_Driver_LastName & "-" & e_Driver_FirstName, because last 500 we had a father and son drive the same car.  Though something like that is rare, that might work.  Thanks Fernando!

Eric
No problem, glad to help. ;=)
Okay guys, I'm going to split the points 50/50, and here's why:

ZeonFlash - You answered first (which usually gives me the indication who gets the majority of points); however, Fernando gave me code that I could cut and paste into my project, and followed up on a clarification question.

I hope that's fair to both of you!

Regards,
Eric
Not a problem, Thanks. ;=)
Sorry I didn't check back earlier for the duplicate key fiasco.  Glad you got an answer that works for you :)
ZeonFlash,

No worries, I just hope you're okay with the points distribution.