user1739123
user1739123

Reputation: 93

Visual Basic: Index was outside bounds of array

I'm trying to get this Visual Basic program to store values in a multi-dimensional array. All other errors I've seen due to an index being outside bounds of array have been due to the fact that the array is declared before the variable, but I'm pretty sure I declared mine in the right place. Can anyone see what's wrong?

    Randomize()
    Dim roll As Integer = 0
    Dim player As Integer
    Dim index As Integer

        Console.Writeline("Enter the number of players: ")
        index = Convert.ToInt32(Console.Readline)

        player = index - 1


        Dim players(player,roll) As Integer

        Do Until index = 0

            Do Until roll = 5
                players(player,roll) = CInt(Int((6 * Rnd()) + 1))
                roll +=1
            Loop

            player -=1

        Loop

Upvotes: 0

Views: 829

Answers (2)

John Bustos
John Bustos

Reputation: 19544

I'd add in a new variable - Try changing your code to this:

Dim MaxRolls as integer
MaxRolls = 5

Then:

Dim players(player,MaxRolls) As Integer 

That should fix it

Upvotes: 1

RBarryYoung
RBarryYoung

Reputation: 56725

You dimension players() with roll=0, but then loop until roll=5. That's obviously outside the bounds of your array.

Upvotes: 3

Related Questions