我收到了Obj
带有一个名为Number
. 这些对象的数组由索引 0..39 中的 40 个对象定义。我不需要 index 处的对象0
。
该代码有一个静态变量,用于保存数组中所有Number
' 的总和Obj
。这个变量只初始化一次。在Number
值本身被定义之后。
它通常有效,但有时我用不同对象的值记录一个异常(我无法重现),我看到那里的值NumberAll
是错误的(我最近记录了 1722 和 2134。没关系- 但他们唯一常见的主要因素是 2。应该是 1554,如果你想知道,你可以在下面查看我 :) )。
编辑:异常是Index was outside the bounds of the array
在下面的代码中引入的循环 - 这个问题的直接影响。
我尝试了两种求和方法,如下所示。
相关代码如下。代码中没有其他地方可以为 赋值NumberAll
。
Static NotFirstTime As Boolean, NumberAll As Integer
If NotFirstTime = False Then GoTo Data
sPoint: 'Code and more code
' ...
' ...
breakValue = someValue Mod NumberAll
Sum = 0
I = 1
Try
Do
If Sum + myArray(I).Number >= breakValue Then
Exit Do
End If
Sum = Sum + myArray(I).Number
I = I + 1
Loop
Catch ex As Exception
'log error and variable data - breakValue is larger than Sum will ever get
'hence the loop reaches the point of trying myArray(40) which doesn't exist
End Try
' ...
Data:
NotFirstTime = True
myArray(1).Number = 37
myArray(2).Number = 34
myArray(3).Number = 44
myArray(4).Number = 31
myArray(5).Number = 59
myArray(6).Number = 26
myArray(7).Number = 33
myArray(8).Number = 28
myArray(9).Number = 20
myArray(10).Number = 13
myArray(11).Number = 92
myArray(12).Number = 65
myArray(13).Number = 71
myArray(14).Number = 22
myArray(15).Number = 22
myArray(16).Number = 42
myArray(17).Number = 26
myArray(18).Number = 26
myArray(19).Number = 33
myArray(20).Number = 34
myArray(21).Number = 22
myArray(22).Number = 19
myArray(23).Number = 85
myArray(24).Number = 72
myArray(25).Number = 47
myArray(26).Number = 40
myArray(27).Number = 47
myArray(28).Number = 54
myArray(29).Number = 48
myArray(30).Number = 44
myArray(31).Number = 37
myArray(32).Number = 34
myArray(33).Number = 44
myArray(34).Number = 9
myArray(35).Number = 57
myArray(36).Number = 37
myArray(37).Number = 19
myArray(38).Number = 13
myArray(39).Number = 68
NumberAll = 0
'Only one of the following methods is used. They seem to give the same result. The bug exists in both.
'Method one
For I = 1 To 39 'E מספר המסכתות
NumberAll = NumberAll + myArray(I).Number
Next I
'Method two
Dim myList As List(Of Obj) = New List(Of Obj)(myArray)
NumberAll = myList.Sum(Function(b) b.Number)
NumberAll -= myArray(0).Number
GoTo sPoint