Declaring Variables

Declaring Variables

When you declare a variable in VB6 you use the dim statement. VB .NET supports the dim keyword. The difference is that you can and are encouraged to dim variables, create instances of them, and provide initial values all on the same line in VB .NET.

Declaring and creating an instance is referred to as instantiation . Here are several examples of declaring variables, followed by examples of instantiating objects.

 Dim I As Integer = 5 Dim S As String = "Welcome to Valhalla Tower Material Defender!" Dim ADate As DateTime = DateTime.Now Dim Objects As New Object(){1, 2, DateTime.Now, "Some Text"} Dim Log As EventLog = New EventLog() 

The first statement declares an integer I initialized to 5. The second statement initializes a string variable and initializes it to "Welcome to Valhalla Tower Material Defender!" The third statement declares a DateTime structure and initializes it to the current date and time. The fourth statement is more complex; it declares an array of Object and initializes the array to heterogeneous values, demonstrating the initializer list idiom. The last statement declares and initializes an EventLog class and instantiates the EventLog object Log .

Several things may jump out at you when examining these examples. First, every statement has an initial value. We have been telling programmers to provide initial values for years . VB .NET is not COM-based VB6, and you are encouraged to provide initial values at the point of declaration. Second, you might notice there is no Hungarian prefix notation or any other notation. Admittedly, I and S are not great variable names ; the point is that prefix notations are discouraged, even by Microsoft, because the reason they were used in the first place no longer exists. Prefix notations were employed for weakly typed languages like C, not strongly typed languages like VB .NET. In addition to avoiding prefixes, you should add the statements Option Explicit On and Option Strict On to the beginning of every module, or at the project level, to ensure that variables are declared and everything is bound early.

The last thing you need to know is that every single variable in the list of examples is an object. That means each variable actually may have one or more fields, properties, methods , or events. Collectively these are referred to as members . You can find out what the members are by (a) looking them up in the integrated help documentation or (b) typing the variable or type name followed by the dot operator ( . ), which will prompt Intellisense to provide you with a list of members. For example, typing "I." in the code editor will list the members of the Integer type.


Intellisense is a technology that uses Reflection to provide a dynamic list of the members of classes in Visual Studio .NET. Intellisense is a great time-saver when it comes to learning about the .NET Framework.

In the Options dialog, on the Text Editor, All Languages, General item, you can uncheck the Hide advanced members option and Intellisense will provide you with an expanded list of members.

Some of the variables in the five statements listed earlier are referred to as value types and others are referred to as reference types . There is a distinction in the ways value types and reference types are created and managed; to work effectively in VB .NET you need to know this information, which is presented in the next section.

Visual Basic. NET Power Coding
Visual Basic(R) .NET Power Coding
ISBN: 0672324075
EAN: 2147483647
Year: 2005
Pages: 215
Authors: Paul Kimmel © 2008-2017.
If you may any questions please contact us: