System.IndexOutOfRangeException问题 [英] Problem with System.IndexOutOfRangeException

查看:139
本文介绍了System.IndexOutOfRangeException问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我是VB的初学者,我正在尝试制作一个非常简单的程序。我非常肯定我的代码不够优雅且低效,但请耐心等待。



这是我代码的一部分。我试图让它尽可能短,但它仍然很长(我省略'1/3 4套2',因为那里没有问题,2/3和3/3非常相同)。但是,代码在此行崩溃,第三行为3/3:



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





我得到的错误信息是发生了'System.IndexOutOfRangeException'类型的未处理异常。我不确定为什么会发生这种情况,因为代码实际上和以前一样。当我试图删除该行时,同样的错误发生在aTw(10,0)= b2 - c2所以我假设aTw的第一个索引不能超过8,这很奇怪,因为它应该达到11.



非常感谢你,再次为超长代码感到抱歉。





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

推荐答案

声明定义为;

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



但你是r eferencing 声明(某事, 2



尝试声明声明 as;


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

Try declaring the statement as;

Dim statement(287, 2) As String





希望这会有所帮助,

Fredrik



Hope this helps,
Fredrik


这篇关于System.IndexOutOfRangeException问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆