0

I received code with an object (we'll call it Obj) with a property named Number. An array of these objects is defined with 40 objects in indexes 0..39. I don't need object at index 0.

The code has a static variable saving the sum of all Number's of Obj's in the array. This variable is initialized only once. Right after the Number values themselves are defined.

It usually works, but sometimes I log an exception (which I haven't been able to reproduce) with values of different objects and I see there the value of NumberAll is wrong (I've recently logged 1722 and 2134. Not that it should matter - but the only common prime factor for them is 2. should be 1554, if you were wondering, you can check me below :) ).

EDIT: The exception is an Index was outside the bounds of the array on the loop brought in the code below - a direct effect of this issue.

I've tried two methods of summing, brought below.

Relevant code follows. There is nowhere else in the code that a value is assigned to 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
JNF
  • 3,696
  • 3
  • 31
  • 64
  • Are you sure that `myArray(0)` does not exist and has a random value? – Marco Oct 23 '12 at 07:37
  • @Marco Exists but is irrelevant. It holds an object I don't want to include in sum. I'll make that edit to the code in the question. – JNF Oct 23 '12 at 07:41
  • Concerning your use of `GoTo` http://stackoverflow.com/questions/46586/goto-still-considered-harmful – Dennis Traub Oct 23 '12 at 07:45
  • @DennisTraub I'm all for that. This is external code I received and I don't want to make unnecessary changes. – JNF Oct 23 '12 at 08:02
  • Is there any multithreading involved? Does this only happen on your computer - is it overclocked or prone to unexpected behaviour? Incidentally, your Method two would be tidier as `NumberAll = myArray.Skip(1).Sum(Function(b) b.Number)`. – Andrew Morton Oct 23 '12 at 08:59
  • @AndrewMorton thanks. No multithreading I'm aware of. It happens on a web site. – JNF Oct 23 '12 at 18:38
  • Can you refactor it to eliminate the GoTo? And what causes the exception to be logged - could that be where the error is? – Andrew Morton Oct 23 '12 at 20:14
  • @AndrewMorton, I might refactor, but I'd rather not at the present. I edited the part which throws the exception into the code above. – JNF Oct 24 '12 at 06:33
  • Have you right-clicked on NumberAll and used "Find All References"? It's more reliable than looking through the code. Variables tend not to change if you don't change them. Also, you're checking that sum(myArray(I).Number) (where I is in the range [1,39]) has not decreased from the initial sum: is that the intent? – Andrew Morton Oct 24 '12 at 18:17
  • @AndrewMorton, that's exactly what I did, and that's the intent. – JNF Oct 25 '12 at 09:34
  • In that case, I suspect the code writing to the log is wrong, e.g. writing `"NumberAll: " & someValue.ToString`. – Andrew Morton Oct 25 '12 at 09:53
  • @AndrewMorton, `"NumberAll=" & NumberAll` is the code we're using. Thanks for staying on this :) – JNF Oct 25 '12 at 10:27
  • If it compiles with `"NumberAll=" & NumberAll` then it's time to use `Option Strict On`: one of the errors it shows may point to where things are going wrong. – Andrew Morton Oct 25 '12 at 11:39
  • Removed Goto. Now it seems `NotFirstTime` is `true` even though `NumberAll` is 0. This shouldn't happen (of course). I'm assuming it has to do with them being static which should be a totally different question, I believe. – JNF Oct 29 '12 at 07:21

0 Answers0