Why am i getting this error again and again in the fibonacci series program?

kdskamal

Member
Joined
Jul 21, 2007
Messages
5
Programming Experience
3-5
I m making a fibonacci series program(0..1..1..2..3..5..) in which I m required to search a given number(input done from the user in textbox) from the listbox.

Here is the error which I get,
VB.NET:
"An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll

Additional information: Specified argument was out of the range of valid values.
"
I've attached the file given below. Can someone pinpoint where am i going wrong?
 

Attachments

  • fibonacci.zip
    7.3 KB · Views: 22
Last edited by a moderator:
I think you've got an "index out of range" error, and I think it's in this section for the Button2_Click event:
VB.NET:
For i = 0 To ListBox1.Items.Count()
            If y = Val(ListBox1.Items.Item(i)) Then
                z = True
                Exit For
            End If
        Next
Since it's a zero-based index, try using "For i = 0 to ListBox1.Items.Count - 1".
 
You're welcome. I'm always happy when I see a post here that I can actually help with instead of asking for help. Your code also has an issue with including the next number after the limit in the listbox. I can show you the solution to that one too, if you want it. I don't know if this is a school project. Anyway, I'd have to wait until I got home from work (in a few hours) to post the code.
 
Hey thanks blake!

I m just learning vb.net these days. Its little different from VB and C++(and java)

Anyway, with the problem you mentioned, will the following code be fine?

VB.NET:
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim y As Integer
        Dim z As Boolean
        Dim ask As String
        z = False
        y = Val(TextBox1.Text)
        Dim i As Integer
        For i = 0 To ListBox1.Items.Count - 1
            If y = Val(ListBox1.Items.Item(i)) Then
                z = True
                Exit For
            End If
        Next
        If z = True Then
            MsgBox("found")
        Else
            MsgBox("not found")
        End If
        ask = MsgBox("do you want to clear the listbox?", MsgBoxStyle.YesNo + MsgBoxStyle.Question)
        If ask = vbYes Then
            ListBox1.Items.Clear()
            TextBox1.Clear()
        End If
    End Sub
End Class

PS: in vb.net, do we still use vbYes/vbNo or is there something else here? Mine worked, but, still curious
 
Yes, that will work, and yes, you can still use vbYes and vbNo. It's working just fine in this code.

Actually, the problem I was mentioning is that, running your original code, the last number in the listbox exceeds the maximum limit that you specify, in many cases. For example, enter 1000 as your limit, and you'll see 1597 in the listbox. That was the problem I had the fix for if you wanted it.
 
Sure, get the fix rolling :cool:

:) OK, the problem I mentioned was that, for larger numbers as your limit, it was adding the next number after your limit to the listbox. Here's a quick fix. My addition is in bold (and blue, since it was hard to see the bold).
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        a = 0
        b = 1
        ListBox1.Items.Add(a)
        ListBox1.Items.Add(b)
        x = InputBox("enter the limit")
        While c < x
            c = a + b
            [COLOR=Blue][B]If c <= x Then[/B][/COLOR]
                ListBox1.Items.Add(c)
           [COLOR=Blue][B] End If[/B][/COLOR]
            a = b
            b = c
        End While
        Me.TextBox1.Visible = True
    End Sub
 
Back
Top