TroyPilewski
TroyPilewski

Reputation: 369

How do I fix "index was outside the bounds of the array" and write values to the array

I am trying to write a Role-Playing Dice subprogram. I am using Visual Studio 2010 writing in Visual Basic. All syntax according to Visual Studio is correct. The form looks as follows:

When you click numerical buttons with value of either a "1" or "null" for the # of Dice text boxes it will roll one dice and give you one random value depending on which numerical button you click. If you put a value of 2 or greater then the program stops and gives me the following exception error.

I have done the research and think that the code is written correct but for some reason value are not making it into the array. Would like to know how to make the value enter the array and make the program recognize that more than one dice is being rolled. Below is a copy of the code for one of the button.

Private Sub btnD4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD4.Click
    Dim v, w, x, y, z, iarry(x) As Integer
    lvRolls.Items.Clear()
    If txt4Qty.Text = vbNullString Then
        x = 1
    Else
        x = CInt(txt4Qty.Text)
    End If
    If x = 1 Then
        z = CInt(Int(Rnd() * 5))
        If z > 4 Then
            z = 4
        ElseIf z < 1 Then
            z = 1
        End If
        lvRolls.Items.Add("Roll 1")
        lvRolls.Items(0).SubItems.Add(CStr(z))
        If txt4Mod.Text = vbNullString Then
            lblTotal.Text = CStr(z)
        Else
            w = CInt(txt4Mod.Text)
            lblTotal.Text = CStr(z + w)
        End If
    Else
        For y = 0 To x Step 1
            z = CInt(Int(Rnd() * 5))
            If z > 4 Then
                z = 4
            ElseIf z < 1 Then
                z = 1
            End If
            iarry(y) = z
        Next
        For v = 0 To x
            lvRolls.Items.Add("Roll " & v + 1)
            lvRolls.Items(x).SubItems.Add(CStr(iarry(y)))
        Next
    End If
End Sub

Upvotes: 0

Views: 3778

Answers (2)

Henry Rodriguez
Henry Rodriguez

Reputation: 815

I recomend you change the array and use list(of integer), it's easyer to work with and you don't need to initialize the number of items of the list.

Edit:

Here are a version of your code, i don't know if is this what you want.

 Private Sub btnD4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD4.Click

    Dim x As Integer = CInt(If(txt4Qty.Text <> vbNullString, txt4Qty.Text, 1))
    Dim iarry As New List(Of Integer)

    lvRolls.Items.Clear()

    If x = 1 Then

        Dim z As Integer

        z = (New Random).Next(1, 4)

        lvRolls.Items.Add("Roll 1")
        lvRolls.Items(0).SubItems.Add(z.ToString)

        If txt4Mod.Text = vbNullString Then
            lblTotal.Text = z.ToString
        Else
            lblTotal.Text = (z + CInt(txt4Mod.Text)).ToString
        End If

    Else

        Dim i As Integer

        For i = 0 To x
            iarry.Add((New Random).Next(1, 4))
        Next

        For v = 0 To x

            Dim t_item As New ListViewItem("Roll " & v + 1)

            lvRolls.Items.Add(t_item)

            t_item.SubItems.Add(CStr(iarry(v)))

        Next

    End If
End Sub

Upvotes: 1

Brian Hooper
Brian Hooper

Reputation: 22044

You are declaring your array iarry here:-

Dim v, w, x, y, z, iarry(x) As Integer

This will have the effect of declaring the array to have no elements. You need, as soon as the value of x is known, to

ReDim iarry(x)

Edit perusing your code a little more carefully, it seems you may need

ReDim iarry(x + 1)

Dimensioning an array of x elements gives you elements 0, 1, 2, ..., x-1.

Upvotes: 3

Related Questions