How to make a search/find through array

BlueOysterCult

Regular
Joined
Oct 3, 2003
Messages
84
Hello again all
If I have asked this question already - I am sorry I can't remember if I did or not

I would like to add a "search/find artist" (file in the array) to my CD Collection program- I am not sure even where to start - some sort of .Subtring? That's all my brain can come up with. Maybe a boolean type thing? I don't know- Can anyone give me any ideas?

All I have so far of course is:
Public Function FindArtist() As Integer


Rob
 
Whether you have an array or an arraylist for your artists..

I am assuming you have an array of strings

Public Function FindArtist(Byval SearchCriteria as String) as integer
Return YourArray.IndexOf(SearchCriteria)
End Function

This would return the index of the object you are looking for, -1 if it doesnot exist.

Hope this helps,
 
Yes it does help thanks
my array loos like this
Code:
Public Sub LoadCDArray()

        Dim intCdCount As Integer

        'Read file into array
        Do Until EOF(gintCdFileNum)
            If intCdCount + 1 = Int(intCdCount + 1) Then
                ReDim Preserve CdArray(intCdCount + 1)

            End If

            ' Load the data in a loop with fields

            Input(gintCdFileNum, CdArray(intCdCount).strArtist)
            Input(gintCdFileNum, CdArray(intCdCount).strTitle)
            Input(gintCdFileNum, CdArray(intCdCount).strYear)
            Input(gintCdFileNum, CdArray(intCdCount).strCategory)

            intCdCount += 1
        Loop
        ReDim Preserve CdArray(intCdCount - 1)

    End Sub

I aske d a frien about the first line in the Find func - he said soemthing about the params being - byval ... and byref ....

what would be the SearchCriteria? "," ?

Rob
 
Search Criteria is the parameter you are passing. It depends on what you want to search for (or actually what is stored in your array), ArtistName for example...
 
Uggg
Been working on this Find/Search for a while....

I feel like I am closer but then again..

CAN someone help?
Code:
    Public Function FindArtist(ByVal strArtistToFind As String, ByRef intCurrent As Integer) As Integer

        Dim strFindWhat As Boolean
        Dim intSearch As Integer
        intSearch = intCurrent
        strFindWhat = False
        Do Until strFindWhat = True
            If CdArray(intSearch).strArtist Then
                strFindWhat = True

            End If
        Loop
      

        Else
            MsgBox("The data you are searching for does not exist")

I know its rough but thsi searching thing is hard

Rob
 
Well if you are using an array of a class defined by you, then i doubt you can use the find method, try this
Code:
 Public Function FindArtist(ByVal strArtistToFind As String) As Integer

Dim i as integer
'loop through the array
For i = 0 to cdArray.Length - 1
      If cdArray(i).strArtist = strArtistToFind Then
             'we found the artist, return his index
             Return i
      End If
Next

'once this code is executed we know we didnot find an artist(if we had found one we would have returned above)
Return -1

End Function

Check if the returned index is -1 then give the user a message or something.

Hope this helps,
 
Thank you Mehyar
When I call the function in the Form I have an error that says
"Argument not specified for parameter strArtistToFind"

I had that before and it is reffering to the (ByVal strArtistToFind As String) (?)

Rob
 
How are you calling the Function ?

You should have the parameter you want to search for specified..
ex:
Dim ArtistIndex as Integer = FindArtist("JohnLennon")
 
Private Sub mnuActionFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuActionFind.Click

FindArtist()

End Sub

If I dim ArtistIndex as int = findArtist("jonh lennon")
How does that give the user a way to put in the search?
R
why is this confusing to me?
 
Ok, in your form you shoud have sort of a text box for the user to enter the artist he wants to search for.

Then the index would be Dim i as integer = findArtist(txtArtist.Text)
 
I have tried every combination of this and I am still getting the parameter error

Code:
Public Function FindArtist(ByVal strArtistToFind As String) As Integer
        Dim ArtistIndex As Integer = FindArtist(txtArtist.Text)
        Dim i As Integer
        'loop through the array
        For i = 0 To CdArray.Length - 1
            If CdArray(ArtistIndex).strArtist = strArtistToFind Then
                'we found the artist, return this index
                Return i
            End If
        Next

        'once this code is executed we know we didn't find an artist
        Return -1

    End Function
End Class

Rob ;-(
 
This should be your function
Code:
Public Function FindArtist(ByVal strArtistToFind As String) As Integer
         Dim i As Integer
        'loop through the array
        For i = 0 To CdArray.Length - 1
            If CdArray(ArtistIndex).strArtist = strArtistToFind Then
                'we found the artist, return this index
                Return i
            End If
        Next

        'once this code is executed we know we didn't find an artist
        Return -1

    End Function

'to use it, assume the user typed an artist name in your textbox
'you want to search for that artist

in your form for example on the click of a button
Dim index as integer = FindArtist(txtArtist.Text)
If index <> - 1 Then
'do what ever you want here
Else
MessageBox.Show("Artist not Found")
End If
 
Back
Top