Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Dim strX as string gets old values!

I have some code where in a loop a string is declared:

for ix as integer = 1 to 10
  dim strX as string

We do not use dim strX as string="" because I thought that the dim statement would always default to the empty string. Maybe this is a bad programming practice but I didn't think it was required these days.

Anyway, we have a problem where after the dim statement we find that strX has the value that it had last time round the loop!

In a large 50,000+ line system this has only happened in two areas in the code. But it happens consistently in those two areas.

Anyone else seen this problem? Has anyone seen documentation saying that the Dim statement should initialise variables to 0, empty string, Nothing etc. as appropriate?


Harry McBain
Wednesday, April 13, 2005

The wonder of Google.

Look for the section starting with "Tricky Scope". Following the links to the microsoft docs didn't help but it seems that this is a known "feature".

Did you expect it to work like this though??????

Harry McBain
Wednesday, April 13, 2005

Assuming you're talking about VB, it works as it always has and this is what it is meant to do.

Joel Spolsky
Wednesday, April 13, 2005

It's very poorly documented behaviour and not at all obvious. You learn that when a variable goes out of scope it is automatically destroyed.

Harry McBain
Wednesday, April 13, 2005

But it doesn't go out of scope, does it?

some guy who only thinks he knows what he's talking about
Wednesday, April 13, 2005

Yeah, it didn't go out of scope though, so obviously it keeps the same value.

It isn't a "quirk" either, it is behaving exactly as it should, as reflected in the documentation, if you bothered to RTFM.

This really is CS101 stuff.

Joel Spolsky
Wednesday, April 13, 2005


When I did RTFM about the Dim statement it says:

"The Dim statement can declare the data type of a variable and initialize its contents. The declaration statements in the following example declare an Integer variable, a Boolean variable, and an object variable. These are initialized, respectively, to 10, True, and a newly created instance of the Label class in the System.Windows.Forms namespace.

Dim Quantity As Integer = 10
Dim FirstTry As Boolean = True
Dim MyLabel As New System.Windows.Forms.Label

If you do not specify an initialization value for a variable, Visual Basic initializes it to the default value for its data type. The default initialization values are as follows:

0 for all numeric types (including Byte).
Binary 0 for Char.
Nothing for all reference types (including Object, String, and all arrays).
False for Boolean.
12:00 AM of January 1 of the year 1 for Date. "

It doesn't say when it will initialise the variable so it's reasonable to assume that it will be when that instruction is "executed".

Does the same thing happen with the equivalent code in c# ?
i.e. for (int i=0;i<=10;i++) { String strX .... }
(I forget the exact syntax)

it does seem non-obvious.
in c# is would be even more bizarre as we consider the curly braces to show scope and sometimes (in a method) exiting the curly brace section descopes variables whereas other times (for, if etc) exiting the curly brace section does not descope variables

CS101? I doubt it. VB101 maybe.

Harry McBain
Wednesday, April 13, 2005

Sadly, due to the heritage of the languages, the scoping/lifetime rules for VB and C# are very different.

Joel Spolsky
Thursday, April 14, 2005

Well, technically a loop iteration in C# does exit the scope... it goes to the loop condition which is written outside the {} block!

Another way to think about it is that a variable is re-initialized whenever its declaration statement is encountered. Personally I find the C# scoping rules very obvious but then I'm coming from a C/C++ background.

Chris Nahr
Thursday, April 14, 2005

In that link posted above, never mind variable scope in a loop; check out what he says one or two blog entries down about optional parameters.  Now that's disturbing.

Saturday, April 16, 2005

Here, that is:

Sunday, April 17, 2005

*  Recent Topics

*  Fog Creek Home