What's Wrong With this Code? (#3)

Here is a Visual Basic program with bowling scores in a multi-dimensional array. Two players bowled three games each. The program tries to display the total score for each bowler, but has a small problem. Can you spot it?

Module Module1

  
Sub Main()

    
Dim allScores As Integer(,) = _
        {{101, 128, 143}, {123, 115, 116}}

    
' for each player
    For i As Integer = 0 To allScores.GetUpperBound(0)

      
Dim score As Integer

      ' for each game
      For j As Integer = 0 To allScores.GetUpperBound(1)
        score = score + allScores(i, j)
      
Next

      Console.WriteLine("Player {0} score: {1}", i + 1, score)

    
Next

  End Sub

End
Module

posted on Monday, August 14, 2006 10:30 PM by scott

Comments

Monday, August 14, 2006 8:07 PM by Eber Irigoyen

# re: What's Wrong With this Code? (#3)

due to the scope, variable score needs to be initialized to zero
Monday, August 14, 2006 8:13 PM by Abhijit

# re: What's Wrong With this Code? (#3)


variable score should be initialised to 0

Dim score As Integer = 0
Monday, August 14, 2006 8:39 PM by Geoff Appleby

# re: What's Wrong With this Code? (#3)

Ha, an easy one :)

score might look like it's declared inside the first loop, but when compiled the declaration moves to the top of the mothod.

The end result is that the printed score for player 2 is the actually the entire sum.

By modifying it to be Dim score as Integer = 0 (or exlicitly assinging 0 on the next line) the declaration is still at the top, but the assignment to 0 happens on every iteration.
Monday, August 14, 2006 8:48 PM by scott

# re: What's Wrong With this Code? (#3)

You guys nailed it! Too easy!
Monday, August 14, 2006 9:27 PM by Justin King

# re: What's Wrong With this Code? (#3)

It's not written in C# :P
Monday, August 14, 2006 9:45 PM by Christopher Steen

# Link Listing - August 14, 2006


Announcing
the Windows Mobile Virtual User Group Meeting [Via: trobbins ]
Refactoring
...
Tuesday, August 15, 2006 12:11 AM by James

# re: What's Wrong With this Code? (#3)

It is written in Visual Basic?

:o)
Tuesday, August 15, 2006 4:54 AM by scott

# re: What's Wrong With this Code? (#3)

James, Justin:

In C# this would yield a "use of unassigned local" warning. In a sense you are right.
Tuesday, August 15, 2006 7:43 AM by Eric D. Burdo

# re: What's Wrong With this Code? (#3)

Geoff... VB Classic doesn't have scope level variables.

VB.Net does. So the declaration doesn't get moved to the top. It stays inside the block it is declared in.

So you need to either a) Declare the score at the top, then initialise to 0 inside the loop, or b) initialise to 0 inside the loop when you declare the score variable.

I would prefer method b if I didn't need the score outside of the first loop.
Tuesday, August 15, 2006 1:37 PM by marshall

# re: What's Wrong With this Code? (#3)

can anyone tell me a reason why vb.net behaves like this?

i can't see any good reason for keeping a variables value when it goes out of scope.