oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

VB.NET Data Types
Pages: 1, 2, 3


Forget the Date and Time statements in VB6. VB7 now uses the DateTime structure in the System namespace to represent a date and time value. This structure has a large number of methods and properties that are more than sufficient to work with to get any date and time value. The most important property is probably Now, which retrieves the current date and time. And then there are Day, Date, Month and Year properties that you can use to display the date in any format. For example, the following code displays the current date and time in the "dd/mm/yyyy" format.

imports System
Module Module1
  Sub Main()
    Dim now As DateTime
    now = DateTime.Now
    Console.writeline(now.Day & "/" & _
      now.Month & "/" & now.Year)
  End Sub
End Module

When using DateTime, you will probably use its Ticks property a lot to retrieve the 100-nanosecond tick count for this instance. This property can replace the Timer function in VB6 when you want to benchmark an operation, as in the following code.

imports System
Dim t1 As Long, t2 As Long
t1 = DateTime.Now.Ticks

' operation to be measured here

t2 = DateTime.Now.Ticks
System.Console.writeline(t2 - t1)

The DateTime structure is also convenient to use for some "date-time mathematics." For example, you can add x hour or y minutes to an instance of DateTime using the AddHours and AddMinutes methods.


As mentioned above, the Object class in VB7 represents the .NET Framework Object class. This class is the root of every other type in the .NET Framework. In VB7, when you create a new class, that new class implicitly inherits from Object. Of particular importance are the GetType and ToString methods. The GetType method enables you to obtain the Type of the object, and the ToString method returns a String that represents the current object.


In VB6, a structure is declared using the Type...End Type construction. The structure and its members all default to public access. Explicit access declaration is optional. The following example shows a valid structure declaration:

Type Employee 
  ID As Integer 
  FirstName As String 
  LastName As String   
End Type

In VB7, the Type statement is not supported. You must declare structures using the Structure ... End Structure construction. Every member of a structure must have an access modifier, which can be Public, Protected, Friend, Protected Friend, or Private. You can also use the Dim statement, which defaults to public access. The structure in the preceding example can be declared as follows:

Structure Employee
  Public ID As Integer ' Must declare access, even if Public.
  Dim FirstName As String ' Still defaults to Public access.
  Private LastName As String ' Can be made Private inside Structure.
End Structure

VB7 unifies the syntax for structures and classes. Structures support most of the features of classes, including methods. Structures can serve as light-weight classes that are cheap to create. However, structures do not support inheritance.

Some Other Changes

The following are some important changes that can ease your migration from VB6.

Option Explicit is Imposed by default
You must now declare a variable before using it.

Declare and Assign in the Same Line
You can now declare and assign a variable in the same line. A long-awaited feature.

Dim myVar As Integer = 9

Multiple Variable Declaration
In VB6, you can declare variables of different types in the same statement, but you must specify the data type of each variable or it defaults to Variant. The following example shows multiple declarations and their resulting data types:

Dim I, J As Integer             ' I is Variant, J is Integer. 
Dim L As Integer, M As Integer  ' L is Integer, M is Integer. 
Dim N As Integer, X As Double   ' N is Integer, X is Double.

In VB7, you can declare multiple variables of the same data type without having to repeat the type keyword. The declarations equivalent to those in the preceding example are as follows:

Dim I                           ' I is Object. 
Dim J As Integer                ' J is Integer. 
' -- OR -- 
Dim I As Object, J As Integer   ' I is Object, J is Integer. 
Dim L, M As Integer             ' L is Integer, M is Integer. 
Dim N As Integer, X As Double   ' N is Integer, X is Double.

External Procedure Declaration
In Visual Basic 6.0, when you declare a reference to an external procedure with the Declare statement, you can specify As Any for the data type of any of the parameters and for the return type. The As Any keyword disables type checking and allow any data type to be passed in or returned.

VB7 does not support the Any keyword. In a Declare statement, you must specifically declare the data type of every parameter and of the return. This improves type safety. You can overload your procedure declaration to accommodate various return types.

Variable Scopes
In VB6, any variable declared inside a procedure has procedure scope, so it can be accessed anywhere else within the same procedure. If the variable is declared inside a block -- that is, a set of statements terminated by an End, Loop, or Next statement -- the variable is still accessible from outside the block. The following example illustrates procedure scope:

For I = 1 To 10 
  Dim N As Long ' N has procedure scope although 
                ' it was declared within a block. 
  N = N + Incr(I) 
W = Base ^ N    ' N is still visible outside the block

In VB7, a variable declared inside a block has block scope, and it is not accessible from outside the block. The preceding example can be rewritten as follows:

Dim N As Long  ' N is declared outside the block 
               ' and has procedure scope. 
For I = 1 To 10 
  N = N + Incr(I) 
W = Base ^ N   ' N is visible outside the block but I is not.

However, the lifetime of a variable is still that of the entire procedure. This is true whether the variable has block scope or procedure scope. If you declare a variable inside a block, and if you enter that block several times during the lifetime of the procedure, you should initialize the variable to avoid unexpected values.

Static Local Variables
In VB6, you can declare a procedure with the Static modifier. This causes every local variable within the procedure to be static and to retain its value between calls. In VB7, the Static modifier is not supported in a Function or Sub statement. You must individually declare each local variable you want to be static.


In this article, you have learned the new data types in VB7 that are different from VB6. These are not the only changes in the language, but hopefully this is a good start for VB6 programmers to begin understanding VB7.

Budi Kurniawan is a senior J2EE architect and author.

Return to the .NET DevCenter.