Microsoft KB Archive/311331

= The behavior of variable declaration changes in Visual Basic 2005 and in Visual Basic .NET =

Article ID: 311331

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q311331



SUMMARY
The behavior of variable declaration has changed in Microsoft Visual Basic 2005 and in Microsoft Visual Basic .NET. This article describes the three major differences between Visual Basic 2005, Visual Basic .NET, and previous versions of Microsoft Visual Basic:
 * Multiple declarations on the same line of code
 * Default data type
 * Variable scope within code blocks



Multiple declarations on the same line of code
In previous versions of Visual Basic, when multiple declarations appear on the same line, you must explicitly declare all variables with an AS clause to explicitly type those variables. To type all variables explicitly on the same line in Visual Basic 2005 and in Visual Basic .NET, you can declare a type for only the last variable.

In Visual Basic 6.0, the declaration Dim x,y as String results in variable x as type Variant and variable y as type String.

In Visual Basic 2005 and in Visual Basic .NET, the declaration Dim x,y as String results in variable x as type String and variable y as type String.

Default data type
If you declare variables without an AS clause, the default data type is changed from Variant to Object.

In Visual Basic 6.0, the declaration Dim x results in variable x of type Variant.

In Visual Basic 2005 and in Visual Basic .NET, the declaration Dim x results in variable x of type Object.

Variable scope within code blocks
Visual Basic 2005 and Visual Basic .NET introduce a new scope level. If you declare a variable within a code block in Visual Basic 2005 or in Visual Basic .NET, the scope of that variable is the code block and not the function in which the code block resides.

In Visual Basic 6.0, the following testString variable is in scope anywhere within the function where the code appears: For i = 0 to 1 Dim testString as String testString = &quot;testing&quot; Next i In Visual Basic 2005 and in Visual Basic .NET, the variable testString is in scope only within the For...Next block.

Variables that you declare within a code block lose scope at the end of the block. This applies to iterative blocks such as For...Next, Do...While, and so on. This also applies to logical blocks such as If...Then, Select...Case, and Try...Catch. The following code does not compile in Visual Basic 2005 or in Visual Basic .NET: Sub Main Dim i As Integer For i = 0 To 0

Dim testString As String testString = &quot;testing&quot;

Next i       Debug.WriteLine(testString) End Sub The variable testString appears underlined, and you receive the following error message:

Name 'testString' is not declared.

You receive the same error message with the following code: Sub Main Dim test As Boolean = True If test Then Dim testString As String testString = &quot;testing&quot; End If       Debug.WriteLine(testString) End Sub In Visual Basic 6.0, similar code (for example, change Debug.Writeline to Debug.Print) compiles and runs successfully. This behavior differs because a variable that you declare within a code block is scoped at function level in Visual Basic 6.0 and is scoped at code block level in Visual Basic 2005 and in Visual Basic .NET. This situation does not represent a change in Visual Basic as much as it represents a new and more granular level of scoping in Visual Basic 2005 and in Visual Basic .NET.

Variable declaration, data type conversion, and Option Strict
In Visual Basic 6.0, the declaration Dim x, y as Long indicates that y is declared as a Long and x is declared as the default data type Variant.

When you declare the variable x in this way, you can write the following line of code without any errors in Visual Basic 6.0: x = &quot;Hello world&quot; In Visual Basic 2005 and in Visual Basic .NET, the following code Dim x, y as Integer declares both variables x and y as type Integer. In this case, you receive an error on the following line of code: x = &quot;Hello world&quot; If Option Strict is On, &quot;Hello world&quot; appears underlined in the design environment with the following error message:

Option Strict disallows implicit conversions from String to Integer.

If Option Strict is Off, you receive the following error message at run time:

An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll.

Additional information: Cast from String (&quot;Hello world&quot;) to Integer is not valid.

This error occurs because x is an Integer data type, and the literal on the right side of the assignment statement is a String. This results in a type mismatch.

If you use the statement Option Strict Off, you do not have to specify the data type of a variable explicitly. (However, it is recommended that all declarations specify a data type.) When you do not specify the data type, the variable's type is initially set to the Object data type.

In addition, if you use the statement Option Strict Off, you can convert data types implicitly. Therefore, if you declare x and y as in the above example but do not specify a type Dim x, y you can assign values to x and y as follows: x = &quot;Hello world&quot; y = 123 x = 100 The types are converted when you assign the values.

This is similar to Visual Basic 6.0. In Visual Basic 6.0, if you do not specify a variable at declaration, Visual Basic 6.0 assigns the Variant type automatically. In practice, this means that you can write formulas in a style that is similar to using Variant types in Visual Basic 6.0.

Note Recall that if Option Explicit is On, you must explicitly declare all variables with an AS clause.

You can also declare more than one variable for each statement. To do this, use commas to separate the declarations: Dim x As Long, y as String Note The new rules apply to all variable declarations, regardless of any access modifiers that you specify. Therefore, these rules apply not only to Dim but to Private, Public, Protected, Friend, and so on. Also note that these rules do not apply to the declaration of Function parameter types.

Additional query words: datatype

Keywords: kbvs2005swept kbvs2005applies kbinfo KB311331

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.