Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi everyone!

I'm a beginner at VB and am trying to make a very simple program. I'm very sure that my code is inelegant and inefficient, but please bear with me.

Here is a portion of my code. I've tried to make it as short as possible, but it's still quite long (I omitted '1/3 4 sets of 2' because there are no problems there and 2/3 and 3/3 are very much the same). However, the code crashes at this line, the second line of 3/3:

aTw(9, 0) = b2 * c2


The error message I get is "An unhandled exception of type 'System.IndexOutOfRangeException' occurred." I'm not sure why this happens since the code is practically the same as before. When I tried to remove that line, the same error occured at "aTw(10, 0) = b2 - c2" so I'm assuming the first index for aTw can't exceed 8, which is weird because it should go up to 11.

Thank you very much and sorry again for the super long code.

Dim a, b, c, d, a2, b2, c2, a3, b3, all As Integer
Dim aTh(23, 2), aTw(11, 1) As Integer
Dim statement(287, 1), holder As String
Dim flip As Boolean = False

For i As Integer = 0 To 23
    a2 = aTh(i, 0)
    b2 = aTh(i, 1)
    c2 = aTh(i, 2)

    '2/3 4 sets of 2'
    aTw(4, 0) = a2 + c2
    aTw(4, 1) = b2
    holder = statement(i * 12 + 4, 1)
    statement(i * 12 + 4, 1) = "(" & holder & " + " & c2 & ")"

    aTw(5, 0) = a2 * c2
    aTw(5, 1) = b2
    holder = statement(i * 12 + 5, 1)
    statement(i * 12 + 5, 1) = "(" & holder & " * " & c2 & ")"

    If a2 > c2 Then
        aTw(6, 0) = a2 - c2
    Else
        aTw(6, 0) = c2 - a2
        flip = True
    End If
    aTw(6, 1) = b2
    holder = statement(i * 12 + 6, 1)
    If flip = False Then
        statement(i * 12 + 6, 1) = "(" & holder & " - " & c2 & ")"
    Else
        statement(i * 12 + 6, 1) = "(" & c2 & " - " & holder & ")"
        flip = False
    End If

    If a2 = c2 Then
        aTw(7, 0) = 1
    ElseIf a2 > c2 Then
        If (a2 Mod c2) = 0 Then
            aTw(7, 0) = a2 / c2
        Else
            aTw(7, 0) = 2311
        End If
    Else
        If (c2 Mod a2) = 0 Then
            aTw(7, 0) = c2 / a2
            flip = True
        Else
            aTw(7, 0) = 2311
        End If
    End If
    aTw(7, 1) = b2
    holder = statement(i * 12 + 7, 1)
    If flip = False Then
        statement(i * 12 + 7, 1) = "(" & holder & " / " & c2 & ")"
    Else
        statement(i * 12 + 7, 1) = "(" & c2 & " / " & holder & ")"
        flip = False
    End If

    '3/3 4 sets of 2'
    aTw(8, 0) = b2 + c2
    aTw(8, 1) = a2
    statement(i * 12 + 8, 2) = "(" & b2 & " + " & c2 & ")"

    aTw(9, 0) = b2 * c2
    'The code crashes here'
    aTw(9, 1) = a2
    statement(i * 12 + 9, 2) = "(" & b2 & " * " & c2 & ")"

    If b2 > c2 Then
        aTw(10, 0) = b2 - c2
    Else
        aTw(10, 0) = c2 - b2
        flip = True
    End If
    aTw(10, 1) = a2
    If flip = False Then
        statement(i * 12 + 10, 2) = "(" & b2 & " - " & c2 & ")"
    Else
        statement(i * 12 + 10, 2) = "(" & c2 & " - " & b2 & ")"
        flip = False
    End If

    If b2 = c2 Then
        aTw(11, 0) = 1
    ElseIf b2 > c2 Then
        If (b2 Mod c2) = 0 Then
            aTw(11, 0) = b2 / c2
        Else
            aTw(11, 0) = 2311
        End If
    Else
        If (c2 Mod b2) = 0 Then
            aTw(11, 0) = c2 / b2
            flip = True
        Else
            aTw(11, 0) = 2311
        End If
    End If
    aTw(11, 1) = a2
    If flip = False Then
        statement(i * 12 + 11, 2) = "(" & b2 & " / " & c2 & ")"
    Else
        statement(i * 12 + 11, 2) = "(" & c2 & " / " & b2 & ")"
        flip = False
    End If
Posted
Comments
Bernhard Hiller 18-Sep-14 8:18am    
Dud you try a
- clear solution
- rebuild solution
in Visual Studio?
z18782 18-Sep-14 8:28am    
Thank you for your suggestion. I just tried it, but the result is still the same; it still crashes at the same line.
Bernhard Hiller 18-Sep-14 8:32am    
Very strange. When you add two lines of code before your for loop:
aTw(8,0) = 0
aTw(9,0) = 0
Does it crash on the second line of these extra lines?
z18782 18-Sep-14 8:36am    
Nope... It works fine if it's outside the for loop.
Also, when I tried to add those two lines inside the loop right before the line where the code usually crashes, something even stranger happened: the program crashed at "aTw(8,0) = 0" even though the code before that, "aTw(8, 0) = b2 + c2", works perfectly fine.
z18782 18-Sep-14 8:38am    
I also tried adding "aTw(0, 0) = 0" there but the program crashed again.

1 solution

statement is defined as;
VB.NET
Dim statement(287, 1) As String

But you're referencing statement(something something, 2).

Try declaring the statement as;
Dim statement(287, 2) As String


Hope this helps,
Fredrik
 
Share this answer
 
Comments
z18782 18-Sep-14 10:23am    
I see! So the error does not lie with the line at which the program crashes, the one one before! I was looking for the problem in the wrong place... Thank you so much!
Fredrik Bornander 18-Sep-14 11:35am    
Glad I could help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900