Hey, whenever I compile the code below the program compiles and runs but after a while I get the following error:

System.IndexOutOfRangeException was unhandled
Index was outside the bounds of the array.

Also, when compiling I get the following: A first chance exception of type 'System.IndexOutOfRangeException' occurred in array_mult.exe

The compiler points at

NumberA(i) = Val(NumberA_Char(i))

I don't see why this is happening because 'i' is at max 31, which is acceptable. What I wanted was to read and store numbers in a char array and then convert each element of the char array to int and then store it in the corresponding element of the byte array.

Thanks for reading.

Module Module1

    Sub Main()
        Dim NumberA(31) As Byte
        Dim NumberA_Char(31) As Char

        Dim NumberB(31) As Byte
        Dim NumberB_Char(31) As Char

        Console.Write("NumberA_Char: ")
        NumberA_Char = Console.ReadLine()
        Console.Write("NumberB_Char: ")
        NumberB_Char = Console.ReadLine()

        For i = 0 To 31
            NumberA(i) = Val(NumberA_Char(i))
            NumberB(i) = Val(NumberB_Char(i))
        Next

        Console.Write("NumberA: ")
        For i = 0 To 31
            Console.Write(NumberA(i))
        Next

        Console.WriteLine()

        Console.Write("NumberB: ")
        For i = 0 To 31
            Console.Write(NumberB(i))
        Next

        Console.ReadLine()
    End Sub
End Module

Change the following -

For i = 0 To 31

to

For i = 0 To 30

Your index starts at zero, which is your first counter. You've referenced 31, but are calling 32 loops in your statements, hence the out of range error.

Change the following -

to

For i = 0 To 30

Your index starts at zero, which is your first counter. You've referenced 31, but are calling 32 loops in your statements, hence the out of range error.

I'm still getting the same error. I changed it to...

For i = 0 To 10

...as well just to test and still got the error.

Also, if I have test(31) doesn't that mean that it has 32 elements since array starts from 0 and the 31 in the brackets defines the top limit so 0 to 31. So I used 32 loops.

Edited 6 Years Ago by SoulReaper1680: n/a

My fault, should have seen it the first time around...

For i = 0 To 31
            NumberA(i) = Val(NumberA_Char(i)) 'NumberA has no value set for it
            NumberB(i) = Val(NumberB_Char(i))
        Next

'Try the following
NumberA = 0
NumberB = 0

For i = 0 To 30
NumberA = i
NumberB = i

NumberA = Val(NumberA_Char)
NumberB = Val(NumberB_Char)

Console.ReadLine 'You should do something with the returned value before going to the next i...
Next i

My fault, should have seen it the first time around...

For i = 0 To 31
            NumberA(i) = Val(NumberA_Char(i)) 'NumberA has no value set for it
            NumberB(i) = Val(NumberB_Char(i))
        Next

'Try the following
NumberA = 0
NumberB = 0

For i = 0 To 30
NumberA = i
NumberB = i

NumberA = Val(NumberA_Char)
NumberB = Val(NumberB_Char)

Console.ReadLine 'You should do something with the returned value before going to the next i...
Next i

NumberA, NumberB, NumberA_Char, and NumberB_Char are arrays so I can't do NumberA = i, NumberA = Val(NumberA_Char), etc. I did try it but my compiler didn't let me compile it.

What I wanted was to read a string of numbers and store it in a char array. Then convert each element of the char array (which would be a digit per element) and place it in the byte array.

I think I have found out why it is causing a problem. I ran some tests and found that if I have, lets say, array(9) as char and then i have array = Console.ReadLine and enter foo, the elements of the array not used become 'inactive'.

Any suggestions?

Edit:
I made it to work.

Module Module1

    Sub Main()
        Dim NumberA(31) As Integer
        Dim NumberA_Char(31) As Char

        Dim NumberB(31) As Integer
        Dim NumberB_Char(31) As Char

        Console.Write("NumberA_Char: ")
        NumberA_Char = Console.ReadLine()
        Console.Write("NumberB_Char: ")
        NumberB_Char = Console.ReadLine()

        Console.WriteLine()

        For i = 0 To Len(NumberA_Char) - 1 'Used Len to find the upper limit -1 because arrays start from 0
            NumberA(i) = Val(NumberA_Char(i))
        Next

        Console.WriteLine()

        For i = 0 To Len(NumberB_Char) - 1 'Same as above
            NumberB(i) = Val(NumberB_Char(i))
        Next

        Console.WriteLine()
        Console.WriteLine()

        Console.Write("NumberA: ")
        For i = 0 To 31
            Console.Write(NumberA(i))
        Next

        Console.WriteLine()

        Console.Write("NumberB: ")
        For i = 0 To 31
            Console.Write(NumberB(i))
        Next

        Console.ReadLine()
    End Sub
End Module

Edited 6 Years Ago by SoulReaper1680: n/a

This was my error as well, Always use -1 to start at 0 in a for loop, sorry for not picking it up earlier.:)

Please mark this as solved at the bottom of the page, thanks.

This question has already been answered. Start a new discussion instead.