BlueOysterCult Posted December 2, 2003 Posted December 2, 2003 I get a stack overflow error in the following code: Help? What is the problem here? 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 Quote
Mehyar Posted December 2, 2003 Posted December 2, 2003 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.. Quote Dream as if you'll live forever, live as if you'll die today
BlueOysterCult Posted December 2, 2003 Author Posted December 2, 2003 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 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 Quote
BlueOysterCult Posted December 2, 2003 Author Posted December 2, 2003 Hey Mehyar I am getting a null reference in the function - its pointing at for i = 0 to CdArray.length - 1 line 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 Quote
Mehyar Posted December 2, 2003 Posted December 2, 2003 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... Quote Dream as if you'll live forever, live as if you'll die today
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.