【问题标题】:how to fix IndexOutOfRangeExecption was Unhandled in visual basic如何修复 IndexOutOfRangeException 在 Visual Basic 中未处理
【发布时间】:2021-06-23 23:01:18
【问题描述】:

如何从这段代码中修复?

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) 处理 btnCalculate.Click 将 a 变暗为 Double = 0 将 j 调暗为整数 = poly.n - 1 将 i 调暗为整数

    For i = 0 To poly.n Step 1
        a = a +((poly.Elmt(j).x + poly.Elmt(i).x) * (poly.Elmt(j).y - poly.Elmt(i).y))
        j = i
    Next

    a = Math.Abs(a / 2)
    MessageBox.Show("Area of The Polygon: " & a & " ")
End Sub

【问题讨论】:

标签: vb.net


【解决方案1】:

如 Young Sun 所写,在 poly.n 之后的 for 循环声明中放入 -1。

    For i = 0 To poly.n -1 Step 1

并且总是检查 j 不小于 0。

    Dim j As Integer = poly.n - 1
    If j < 0 Then Exit Sub

或者把你的计算放到try catch中

        Try
            a = a + ((poly.Elmt(j).x + poly.Elmt(i).x) * (poly.Elmt(j).y - poly.Elmt(i).y))
        Catch ex As Exception

        End Try

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多