Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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..

Dream as if you'll live forever, live as if you'll die today
Posted

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

Posted

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

Posted

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...

Dream as if you'll live forever, live as if you'll die today

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...