Classes for Visual Basic 6.0 Users

Classes are the building blocks of an object-oriented application. A class is a programming construct that represents an object in the system. Designing an application requires designing the objects involved, the information contained by the objects, and what the objects can do. For more information on classes and object-oriented programming, see Object-Oriented Programming in Visual Basic.

Class Modules

Visual Basic 6.0

Visual Basic 6.0 provides support for defining classes through the class module. The definition of a class is stored in a special file type, the .cls file. One class is defined in each class module. Some of the definition of the class is embedded in the .cls file and only editable through the project system.

Visual Basic 2008

In Visual Basic 2008, a class is defined by the Class Statement (Visual Basic), not by a file name. Classes in Visual Basic 2008 are defined in the source code files, .vb files. Multiple classes may be defined in one file. The entire class definition is visible as plain text in the source code.

Global Classes

Visual Basic 6.0

When you create a new class in Visual Basic 6.0, the allowable values of the Instancing property include GlobalSingleUse and GlobalMultiUse. These indicate that other components can invoke the properties and methods of the new class as if they were shared members. An instance of the class is implicitly created the first time one of its members is called.

Visual Basic 2008

Visual Basic 2008 does not support the Instancing property. Visual Basic 2008 provides the same functionality by allowing you to expose standard module members. You can access shared properties and methods of a class using the Imports statement. You can achieve the effect of GlobalMultiUse with Public class access and the appropriate constructor access.

Data Classes

Visual Basic 6.0

Visual Basic 6.0 also supports Data Source and Complex Data Consumer classes.

Visual Basic 2008

There is no direct equivalent in Visual Basic 2008. For information about classes that manipulate data, see How to: Connect to Data in an Object.

Default Members

Visual Basic 6.0

In Visual Basic 6.0, you can specify that any particular method or data member is the default member of a class.

Visual Basic 2008

In Visual Basic 2008, a default member of a class or structure can only be a property that takes one or more arguments. This enables interoperability with other programming languages. For more information, see How to: Declare and Call a Default Property in Visual Basic.

Object Lifetime

Visual Basic 6.0

Object lifetime is determined by the creation and termination of the object instance. The program determines the creation time of the objects it declares, but termination involves a more complex mechanism.

In Visual Basic 6.0, every object instance maintains a reference count. When the last reference to an instance is released and the count goes to zero, the object is terminated immediately.

Visual Basic 2008

In Visual Basic 2008, a destructor is not necessarily called as soon as the last reference is released. This is because the common language runtime maintains a reference tree instead of individual reference counts. The garbage collector traces the reference tree in the background. If it finds an object or group of objects that have no reference from any currently executing code, it calls the destructors of all such objects. It is impossible to predict either the order of this destruction or the time it takes the garbage collector to trace the reference tree. Therefore, the lifetime of an object is indeterminate. For more information, see Object Lifetime: How Objects Are Created and Destroyed and Garbage Collection.

Upgrade Options

The Upgrade Wizard creates a straightforward upgrade of class modules with minimal changes to the code. In addition to the changes listed above, Visual Basic programmers have several new constructs to choose from that are particularly relevant for object-oriented programming. A few are listed here.

  • Constructors   Constructors replace the use of Class_Initialize of Visual Basic 6.0. For more information, see Class_Initialize Changes for Visual Basic 6.0 Users.

  • Default indexed properties   Default indexed properties are used extensively throughout the .NET Framework for accessing collection items. For more information, see Default Property Changes for Visual Basic 6.0 Users.

  • ReadOnly   In Visual Basic 6.0, a property is implicitly read-only if there is no Get procedure. Visual Basic 2008 requires the ReadOnly keyword to explicitly declare this behavior. For more information see Property Statement.

  • Late-binding   Since Visual Basic 6.0 provides late-binding by default, the most likely way to upgrade the code without errors is to set Option Strict to Off. Changing Option Strict to On enforces type-safety at compile time, and can expose subtle errors during compilation rather than at run time. For more information see Option Strict Statement.

  • Method overloading   Method overloading reduces the number of named members of a class. This makes the class more readable and easier to program against. For more information, see Procedure Changes for Visual Basic 6.0 Users.

  • File name   Visual Basic 6.0 requires the class name and file name to match. In Visual Basic 2008, the programmer controls both and can define several classes in one source file. How many classes belong in one file is typically covered by a coding standard.

  • ByRef   The default for parameters in Visual Basic 6.0 is ByRef, so the safest upgrade is ByRef. For more information see ByVal.

  • Return keyword   In Visual Basic 6.0, the function name is used as a variable to return the value of the function. Visual Basic 2008 provides the explicit Return keyword for returning function values. For more information, see Return Statement (Visual Basic) .

  • Operator overloading   You can now define the meaning of addition (+), subtraction (-), and other operators for the classes you define. For more information, see Operator Statement.

  • New operators   The += operator can shorten and simplify a line of code. For more information see Arithmetic Operators (Visual Basic) .

  • Inline definition of new values   Syntax in Visual Basic 2008 allows declaring and setting the value of a new variable all in one line of code. This means that you can enforce data constraints from the time the object is instantiated. For more information, see Dim Statement (Visual Basic).

Rewriting the code is not without risks. Any time the code is changed it needs to be tested again. Also, some keywords in Visual Basic 2008 do not act as exactly as they do in Visual Basic 6.0. For more information, see Upgrading Applications Created in Previous Versions of Visual Basic.

See Also


Early and Late Binding

Class_Initialize Changes for Visual Basic 6.0 Users


Structure Statement

Property Statement

Option Strict Statement

Return Statement (Visual Basic)

'ByVal' and 'ByRef' cannot be combined


Other Resources

Creating and Using Objects

Understanding Classes