Section 3.2. Declaring Variables and Constants

   

3.2 Declaring Variables and Constants

A variable declaration is an association of a variable name with a data type. In and of itself, this does not imply variable creation. However, for nonobject variables, a variable declaration does create a variable. A declaration such as:

 Dim   x   As Integer 

creates an Integer variable named x . We can also write:

 Dim x As Integer = New Integer(  ) 

which emphasizes the role of the constructor function for the Integer data type. (The constructor is the function that VB.NET uses to create the variable.)

When multiple variables are declared on the same line, if a variable is not declared with an explicit type declaration, then its type is that of the next variable with an explicit type declaration. Thus, in the line:

 Dim x As Long,   i   ,   j   ,   k   As Integer, s As String 

the variables i , j , and k have type Integer. (In VB 6, the variables i and j would have type Variant, which is VB 6's default data type.)

VB.NET permits the initialization of variables in the same line as their declaration (at long last!). Thus, we may write:

 Dim x As Integer = 5 

to declare an Integer variable and initialize it to 5. Similarly, we can declare and initialize more than one variable on a single line:

 Dim x As Integer = 6, y As Integer = 9 

Note that in this case, each variable that you declare must explicitly be assigned a data type. You cannot assign each variable an explicit value without explicitly declaring the data type of each variable.

Object variables are declared in the same manner:

 Dim obj As MyClass 

However, this declaration does not create an object variable, and the variable is equal to Nothing at this point. Object creation requires an explicit call to the object's constructor, as in:

 Dim obj As New MyClass(  ) 

or:

 Dim obj As MyClass = New Myclass(  ) 

or:

 Dim obj As MyClass obj = New MyClass(  ) 

Variables and constants can be declared with any of the following access modifiers :

  • Public

  • Private

  • Friend

  • Protected

  • Protected Friend

Note also that the Dim keyword can be used as well, but it often defaults to one of the previously mentioned access modifiers. This is potentially confusing, so the Dim keyword should be used only when required, as it is for local variables.

Access modifiers help to specify the scope and accessibility of the variable. We discuss the meaning of these access variables in detail in Chapter 4.

Constant declarations are analogous to variable declarations and have the form:

   AccessModifier   Const Name As Type = Value 

where AccessModifier is one of the access modifiers defined earlier. Note that when Option Strict is On (the default), all constant declarations must have a declared type.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net