Stack overflow problem.. help?!

BlueOysterCult

Regular
Joined
Oct 3, 2003
Messages
84
I get a stack overflow error in the following code:
Help? What is the problem here?
Code:
Public Function FindArtist(ByVal strArtistToFind As String) As Integer

Dim ArtistIndex As Integer = FindArtist(txtFindBox.Text) >>>>> HERE is the problem<<<<<

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

Thanks

Rob

hi Mehyar
 
Hey Rob,

When I gave you the function I didnot write the statement where the error occurs in the function...

Why did you add it ?

Ofcourse you will have an overflow you are looping indefinitly inside the function. You call it once then when the comipler reaches the first statement it calls it again and so on until your memory is full. Remove that statement from the function. When you need to search for the artist this statement should be in an event handling a button click for example or a menu click or whatever, not inside the function itself.

Hope this makes it clear..
 
I have been doing a lot of copying and pasting.... I am tired of this project..... thanks for your help..

my find button is as follows
Code:
Private Sub mnuActionFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                Handles mnuActionFind.Click, btnFindArtist.Click
        Dim index As Integer = FindArtist(txtFindBox.Text)
        If index <> -1 Then
            txtArtist.Text = CdArray(intCurrent).strArtist
            txtTitle.Text = CdArray(intCurrent).strTitle
            txtYear.Text = CdArray(intCurrent).strYear
            txtCategory.Text = CdArray(intCurrent).strCategory

        Else
            MessageBox.Show("Artist not Found")
        End If
I see now that it has been repeated

Thanks Mehyar
I really appreciate it

Rob
 
Hey Mehyar
I am getting a null reference in the function - its pointing at
for i = 0 to CdArray.length - 1 line

Code:
Public Function FindArtist(ByVal strArtistToFind As String) As Integer

        Dim i As Integer
        Dim ArtistIndex 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
uggg. why is this happening?
Rob
 
Why do you need the variable ArtistIndex, you should test for i

in the statement
If CdArray(ArtistIndex).strArtist = strArtistToFind
It should read
If CdArray(i).strArtist = strArtistToFind

Try this first and then tell me what happens...
 
Back
Top