You are here:About>Computing & Technology>Visual Basic
About.comVisual Basic
A lot has changed in declaring variables in VB.NET
Dim isn't dim. It's pretty bright now.
 Know Even More?
Tell Us About It!
 

Microsoft has finally put priorities right and made Option Explicit the default. Furthermore, you can declare all kinds of things with the Dim statement. Oh, and since VB .NET is 'strongly typed' now, MS doesn't recommend variable prefixes anymore. In other words, we used to be encouraged to name an integer variable something like intMyVariable to ensure that it was clear that the variable was intended to be used an integer. VB .NET doesn't let you mix types anymore, so the prefix is unnecessary now.

Here's more about what all this means.

Option Explicit

For years, authors and other VB gurus have been telling us that it's a very good policy to make sure that you declare all of your variables in a program. You can get the compiler to help you with this by using the Option Explicit statement in your VB 6 program. After you code this statement, using an undeclared variable will trigger a compile error.

In VB.NET, it's still legal to code Option Explicit but it's completely unnecessary since that's the default. (Actually, it's Option Explicit On, but if you just enter Option Explicit, the IDE enters the word On for you.)

If you DO code Option Explicit Off, VB.NET gives your variables the type Object which is not the same as the old variant datatype. The old variant datatype no longer exists. You can prove this to yourself with the following program.


  Option Explicit Off
  Public Class Form1
      Inherits System.Windows.Forms.Form
  #Region " Windows Form Designer generated code "
      Private Sub Form1_Load(... 
          Console.WriteLine("var1 is {0}", VarType(var1))
      End Sub
  End Class

Which gives you ...

var1 is Object

Initialization and Dim

Not only can variables can be declared and initialized in Visual Basic .NET on a single line ...

Dim myVar As String = "This is a string."

... but they can be declared as just about anything.

Values from the command line:

Dim MyString As String = Command()

Note: In Debug, set command line values by selecting Project > Properties > Configuration Properties > Debugging to open a dialog that lets you specify a command line argument as a Startup value.

System defined values:

Dim TodayDate As DateTime = Today()

Arrays of mixed objects:

Dim MyObjectArray() As Object = {3.14159, System.Math.Log10(365)}

Note: The brackets enclose what is called an initializer list idiom.

Instantiation of objects using the New keyword to invoke a constructor for the object in the declaration:

Dim MyObjects As Object() = New Object() {1, 2, DateTime.Today, "George"}

Multiple Variable Declaration

Both VB 6 and VB.NET will let you code this line:

Dim var1, var2, var3 As String

But the result is entirely different.

Dim var1, var2, var3 As Date
         MsgBox( _
         "var1 type is: " & VarType(var1) & vbCrLf & _
         "var2 type is: " & VarType(var2) & vbCrLf & _
         "var3 type is: " & VarType(var3) _
          )
Multiple Dim's on one line

In VB 6, only the last variable was declared as Date. In VB.NET, all of them received the expected Date data type.

In VB 6, this statement is flagged as a syntax error:

Dim var1 as Integer = 100

In VB.NET, not only does the declaration work, but something more complicated does too:

Dim var1 As Integer = 100, _
    var2 As String, _
    var3 = 250, var4 As Double
    MsgBox("Result of multiple" & vbCrLf _
    & "declaration in VB.NET: " & vbCrLf _
    & "var1 = " & VarType(var1) & vbCrLf _
    & "var1 value = " & var1 & vbCrLf _
    & "var2 = " & VarType(var2) & vbCrLf _
    & "var3 = " & VarType(var3) & vbCrLf _
    & "var3 value = " & var3 & vbCrLf _
    & "var4 = " & VarType(var4))
Complex Dim in VB.NET
From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Newsletters & RSSEmail to a friendSubmit to Digg
 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.