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
iarrayin a correct manner.Dim v, w, x, y, z, iarry(x) As Integerx might be zero so the length of your array should as well be zero. Since the only part you access that array is whenx != 1that should be it.