Visual Basic: Loop & Array

grant_cpt

Expert Member
Joined
Mar 12, 2011
Messages
1,323
Reaction score
429
Location
Cape Town
Hi there

Been busy practicing for my practical programming exam. And I've been bumping into this specific bug.

Bu.JPG

Please have a look at my code, and tell me if you spot anything wrong.

Regards
Grant

Code:
Public Class Form1

Dim strProvinces(8) As String
    Dim i As Integer = 0

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim blnFlag As Boolean = False


Do While blnFlag = False
            If String.IsNullOrEmpty(Me.txtInput.Text) Then
                blnFlag = True
                MsgBox("Empty")
                Me.txtInput.Focus()
                Exit Do
            Else
                If Me.txtInput.Text = strProvinces(i) Then
                    blnFlag = True
                    MsgBox("Duplicate Found")
                    Me.txtInput.Clear()
                    Me.txtInput.Focus()
                    Exit Do
                Else
                    If i >= 9 Then
                        blnFlag = True
                        MsgBox("There is only 9")
                        Me.txtInput.Clear()
                        Exit Do
                    End If
                End If
            End If

            strProvinces(i) = Me.txtInput.Text
            lsbDisplay.Items.Add(strProvinces(i))
            i = i + 1
        Loop
        Exit Sub
End Sub
 
The last version of BASIC I've used was GW BASIC, but it looks as though you are checking for i >= 9 after you've accessed strProvinces(9), which is why you are getting the out of bounds exception. Assuming these array indices are 1 based (it's been awhile), you probably just have do the i >= 9 check before the "Me... = strProvinces(i)" check.
 
You get the error because your array has only 8 places so when you refer to position 9 with strProvinces(9) you get an exception as that position doesn't exist in the array.
To fix either change to
Code:
Dim strProvinces(9) As String
or
Code:
 If i >= 8 Then
or move the whole if check block to before you reference the array
Code:
 Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

        Dim blnFlag As Boolean = False


        Do While blnFlag = False
            If i >= 9 Then
                blnFlag = True
                MsgBox("There is only 9")
                Me.txtInput.Clear()
                Exit Do

            Else
                If String.IsNullOrEmpty(Me.txtInput.Text) Then
                    blnFlag = True
                    MsgBox("Empty")
                    Me.txtInput.Focus()
                    Exit Do
                Else
                    If Me.txtInput.Text = strProvinces(i) Then
                        blnFlag = True
                        MsgBox("Duplicate Found")
                        Me.txtInput.Clear()
                        Me.txtInput.Focus()
                        Exit Do

                    End If
                End If
            End If

            strProvinces(i) = Me.txtInput.Text
            lsbDisplay.Items.Add(strProvinces(i))
            i = i + 1
        Loop
        Exit Sub
    End Sub
 
oh.. and for the love of god please don't use "Me" to reference current scope.

And while I'm being a dick, please try to avoid flags:

blnFlag = False

And hungarian notation. .NET is type-safe; you don't need the hint.


... and finally, please assure me this is a .exe, not a website. Because, if it is a website, please.. PLEASE look into ASP.NET MVC.


(disclaimer: I am a dick in general but I do not mean to be a dick to you specifically. You seem like a beginner so my advice is given honestly)
 
He is at school. Don't be hard on him
 
VB is funny If you have an array of 9 provinces you can specify it like this strProvinces(8) and will allow you to reference strProvince(0) to strProvinces[8] e.g. 9 items

but in C# the correct way string [] strProvinces= new string[8] you can only reference position 0 to 7

So if he converted the working code from VB into C# it would throw an exception
 
Top
Sign up to the MyBroadband newsletter
X