Object-Oriented Programming for Visual Basic 6.0 Users
This documentation is archived and is not being maintained.

Object-Oriented Programming for Visual Basic 6.0 Users

Visual Basic 6.0 supports object-oriented language elements and has support for objects distributed in libraries. Visual Basic 2008 extends the support for object-oriented programming by supporting all of its language properties.

The sections below list object-oriented features in Visual Basic 2008, comparing them with the Visual Basic 6.0 implementation where applicable. For each feature, links are provided to in-depth Help pages that can help you learn more.

In Visual Basic 6.0, the keywords Private, Friend, Public, and Static are used to set access levels for declared elements.

In Visual Basic 2008, the keywords Private, Friend, Public, and Static, plus the new keywords Protected and Protected Friend are used to set access levels for declared elements. The access level of a declared element is the extent of the ability to access it— that is, what code has permission to read it or write to it.

For more information, see Access Levels in Visual Basic and Scope in Visual Basic.

In Visual Basic 6.0, limited support for embedded attributes is provided through tools such as the Procedure Attributes in the Visual Basic IDE.

In Visual Basic 2008, an Attribute is a descriptive tag that can be used to annotate type and type member statements, thereby modifying their meaning or customizing their behavior. For example, class statements and class method statements can be annotated with attributes.

Your application and other applications, such as the Visual Basic compiler, can use reflection to access attributes to determine how types and type members can be used.

Attributes can be used to perform Aspect Oriented Programming (AOP) with Visual Basic. An aspect is a part of a program that cross-cuts its business logic. In other words, it is needed to complete the program, but is not necessarily specific to the domain that the program is written for. Isolating such aspects as logging and persistence from business logic is the aim of the aspect-oriented programming paradigm.

For more information, see Attributes Overview in Visual Basic, Attributes Used in Visual Basic, and Common Uses for Attributes.

In Visual Basic 6.0, the Binary Compatibility option allows you to automatically retain class and interface identifiers from a previous version of a component when you compile a new version.

In Visual Basic 2008, the Binary Compatibility option is not supported; instead binary compatibility is accomplished with attributes. This gives you direct control over the information placed in your compiled component, such as class and interface identifiers, virtual table offsets, and any appropriate COM attributes.

For more information, see Binary Compatibility Changes for Visual Basic 6.0 Users.

In Visual Basic 6.0, a class is defined in a class module. A single class module is stored in a special type of file that has a .cls file extension.

In Visual Basic 2008, a class is defined in a Class statement that specifies the name and the members of a class. Class statements are stored in source files. The entire source file can be viewed as plain text.

Multiple Class statements as well as other type statements can be stored in single source file. Visual Basic does not require the name of the source file to match a Class or type defined in the source file.

For more information, see Class Statement (Visual Basic).

In Visual Basic 6.0, the class Initialize event handler named Class_Initialize is used to execute code that needs to be executed at the moment that an object is created.

In Visual Basic 2008, one or more constructors are added to a class to execute code and initialize variables. Constructors are the methods in a class that are named New. The New method can be overloaded to provide multiple constructors with the name New within the same class statement.

For more information, see New (Visual Basic) or Using Constructors and Destructors.

In Visual Basic 6.0, the Nothing keyword is used to disassociate an object variable from an actual object. The Set statement is used to assign Nothing to an object variable.

In Visual Basic 2008, for the majority of the objects that your application creates, you can rely on the garbage collector to automatically perform the necessary memory management tasks. However, unmanaged resources require explicit cleanup. The most common type of unmanaged resource is an object that wraps an operating system resource, such as a file handle, window handle, or network connection.

When you create an object that encapsulates an unmanaged resource, it is recommended that you provide the necessary code to clean up the unmanaged resource in a public Dispose method. By providing a Dispose method, you enable users of your object to explicitly free its memory when they are finished with the object.

When you use an object that encapsulates an unmanaged resource, you should be aware of Dispose and call it as necessary.

For more information, see Cleaning Up Unmanaged Resources or Automatic Memory Management.

In Visual Basic 6.0, a class Initialize event can be handled by a Class_Initialize method to execute code that needs to be executed at the moment an object is created. For example, the values of class data variables can be initialized.

In Visual Basic 2008, The Initialize event and the Class_Initialize handler are not supported. To provide class initialization, add one or more constructor methods to the classes and structures you define.

For more information, see Class_Initialize Changes for Visual Basic 6.0 Users.

In Visual Basic 6.0, data source and complex data consumer classes are used to work with external data such as Microsoft SQL Server databases. A data source class provides data from an external source. A data consumer class can be bound to an external source of data such as a Data Source class.

In Visual Basic 2008, data source, simple data consumer, complex data consumer, and binding classes are used to work with external and internal data.

For more information, see Data Binding and Windows Forms.

In Visual Basic 6.0, any class property can be defined as the default property of the class.

In Visual Basic 2008, the default member of a class or structure can only be a property that takes one or more arguments. Default property members are defined by including the Default keyword in one property declaration statement in a class or structure.

For more information, see Default Property Changes for Visual Basic 6.0 Users.

In Visual Basic 6.0, delegate types are not supported.

In Visual Basic 2008, a delegate type is a form of object-oriented method pointer that allows a method to be invoked indirectly by way of a reference to the method. Delegates can be used to hook up event handlers and to pass a method from one method to another.

Delegates can be used to implement the asynchronous design pattern. For example, using an asynchronous delegate, a program can call a method, which enumerates a large list, while the main program continues to execute. When the enumeration is complete, a callback is made, and the program addresses it.

For more information, see Delegates in Visual Basic or How to: Pass Procedures to Another Procedure in Visual Basic.

In Visual Basic 6.0, the On Error statement is used for unstructured exception handling.

In Visual Basic 2008, both unstructured and structured exception handling are supported. Structured exception handling is a control structure containing exceptions, isolated blocks of code, and filters to create an exception-handling mechanism.

For more information, see Structured Exception Handling in Visual Basic or Choosing When to Use Structured and Unstructured Exception Handling.

In Visual Basic 6.0, the Event, RaiseEvent, and WithEvents keywords are used to declare, raise, and handle events.

In Visual Basic 2008, the Event, RaiseEvent, and WithEvents keywords plus the new AddHandler, RemoveHandler, and Handles keywords are used to declare, raise, and handle events.

The AddHandler and RemoveHandler keywords allow you to dynamically add, remove, and change the event handler associated with an event.

The Handles keyword allows you to define a Handles clause on a method. Handles declares that a procedure handles a specified event.

Events in the .NET Framework are based on the delegate model. The delegate model is based on the object-oriented observer design pattern.

For more information, see How to: Write Event Handlers.

In Visual Basic 6.0, generic types are not supported.

In Visual Basic 2008, generics are used to implement parametric polymorphism in a Visual Basic program. Generic code is written without mention of any specific type and thus can be used transparently with any number of new types.

Visual Basic 2008 supports type parameters on generic classes, structures, interfaces, procedures, and delegates. A corresponding type argument specifies at compilation time the data type of one of the elements in the generic type.

For more information, see Generic Types in Visual Basic.

In Visual Basic 6.0, the value of the Instancing property of a class determines whether a class is private — that is, for use only within one component, or available for other applications to use. It also determines how other applications create instances of the class and how other applications call the class members.

In Visual Basic 2008, the Instancing property is no longer supported.

You can apply the Public keyword to class statements in an assembly to expose classes in that assembly to other assemblies.

You can reference an external assembly such as a class library to enable code in your application to use the Public classes from that class library.

You can use the Imports keyword to import namespace names from referenced projects and assemblies. The Imports keyword can also import namespace names defined within the same project as the file in which the statement appears.

You can apply the Shared keyword to field, property, and method members of classes and structures to implement shared members. Shared members are properties, procedures, and fields that are shared by all instances of a class or structure. The shared members of a class can be accessed without instantiating the class.

For more information, see Access Levels in Visual Basic, References and the Imports Statement, or Shared Members in Visual Basic.

In Visual Basic 6.0, implementation inheritance is not supported.

In Visual Basic 2008, you can implement ad-hoc polymorphism through implementation inheritance. This allows you to define classes that can be used interchangeably by client code at run time, but with functionally different (yet identically named) methods or properties.

You can define base classes that serve as the basis for derived classes. Derived classes inherit, and can extend, the properties, methods, and events of the base class. Derived classes can also override inherited methods with new implementations.

For more information, see Inheritance Basics or When to Use Inheritance.

Visual Basic 6.0 provides polymorphism through multiple ActiveX interfaces. In the Component Object Model (COM) that forms the infrastructure of the ActiveX specification, multiple interfaces allow systems of software components to evolve without breaking existing code.

In Visual Basic 2008, you can implement ad-hoc polymorphism with the .NET Framework's Interface keyword.

Multiple classes may implement the same Interface, and a single class may implement one or more interfaces. Interfaces are essentially definitions of how a class needs to respond. An interface defines the methods, properties, and events that a class needs to implement, and the type of parameters each member needs to receive and return, but leaves the specific implementation of these members up to the implementing class.

Multiple interfaces have the advantage of allowing systems of software components to evolve without breaking existing code.

For more information, see Interface-Based Polymorphism or How to: Create and Implement Interfaces.

In Visual Basic 6.0, arguments are passed to a method by value or by reference. Unless method parameters are explicitly specified by the ByVal or ByRef keyword, arguments are implicitly passed by reference (ByRef).

In Visual Basic 2008, arguments are passed to a method by value or by reference. Unless method parameters are explicitly specified by the ByVal or ByRef keyword, arguments are implicitly passed by value (ByVal).

For more information, see Passing Arguments by Value and by Reference.

In Visual Basic 6.0, method overloading is not supported.

In Visual Basic 2008, Method overloading is used to implement ad-hoc polymorphism in a Visual Basic program. A method is overloaded when more than one version of the method is defined in a class. The overloaded versions differ in their parameters and returns types.

For more information, see Overloaded Properties and Methods and How Visual Basic Provides Polymorphism.

In Visual Basic 6.0, methods can not be overridden.

In Visual Basic 2008, you can use the Overrides keyword to override a method in a derived class to provide a different implementation of a method from the derived class's base class.

For more information, see Overriding Properties and Methods.

In Visual Basic 6.0, a Function method's name is used as the name of a variable to return the Function method's result.

In Visual Basic 2008, you use the Return keyword to return a Function method's result.

Use the Return to return control to the code that called a Sub, Function, or Property method.

For more information, see Return Statement (Visual Basic).

In Visual Basic 2008, the new My feature enables rapid object-oriented programming with Visual Basic by providing entry points to frequently used .NET Framework classes and functions. My provides new high-level .NET Framework classes that pull together related functionality into task-based APIs.

For more information, see Development with My.

In Visual Basic 6.0, the Me keyword behaves like an implicitly declared variable. When a class has more than one instance, Me provides a way to refer to the specific instance of the class where the code is executing.

In Visual Basic 2008, the MyBase keyword provides a way to refer to the base class of the current class instance.

The MyClass keyword provides a way to refer to the current class instance without overrides.

The Me keyword provides a way to refer to the specific instance of a class or structure in which the code is currently executing.

For more information, see Me, My, MyBase, and MyClass in Visual Basic.

In Visual Basic 6.0, using the New keyword inappropriately can cause some unusual re-initializing behavior that could result in errors and excessive memory usage.

In Visual Basic 2008, the New keyword simply allocates space for the object; it does not cause any unusual re- initializing behavior.

Use a New clause in a declaration statement or an assignment statement. When the statement is executed, it calls a constructor method of the specified class, passing any arguments supplied in the New clause. This means that you can enforce data constraints from the time the object is instantiated.

A class has one or more constructors. Constructors are the methods in a method named New. Multiple constructors are defined by overloading the New method in the class. Overloaded constructor methods are described as parameterized constructors.

For more information, see New (Visual Basic).

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

In Visual Basic 2008, object lifetime is non-deterministic; 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 references 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.

For more information, see Lifetime in Visual Basic or Object Lifetime: How Objects Are Created and Destroyed.

In Visual Basic 6.0, operator overloading is not supported.

In Visual Basic 2008, operator overloading allows the behavior of a standard operator (such as *, <>, or And) to be defined for a user-defined class or structure.

For more information, see How to: Define a Conversion Operator.

In Visual Basic 6.0, permissive type checking is used at run time.

In Visual Basic 2008, you can use the OPTION STRICT ON statement to enable strict type checking at design time rather than permissive type checking at run time. By default OPTION STRICT OFF is used.

When converting between data types, the Visual Basic compiler can operate under strict or permissive type semantics. If strict type semantics are in effect (OPTION STRICT ON), only widening conversions are permitted implicitly, and narrowing conversions must be explicit. Under permissive type semantics (OPTION STRICT OFF), you can attempt all widening and narrowing conversions implicitly. Type semantics apply to conversions between all data types, including object types.

For more information, see Type Checking in Visual Basic.

In Visual Basic 6.0, the Property Get, Property Let, and Property Set statements are used to get and set property values.

In Visual Basic 2008, a unified property declaration syntax is used that includes the procedures for getting and setting the property's value. This guarantees consistency of property attributes such as access level and overloading.

For more information, see Property Changes for Visual Basic 6.0 Users.

In Visual Basic 6.0, a class's public interface is the set of its public members.

In Visual Basic 2008, a class's public interface is the set of its public members, but an abstract class may be used to implement sub-typing polymorphism in a Visual Basic program.

An abstract class is similar to the .NET Framework's Interface. An abstract class is one that is designed only to serve as a base class for derived classes. Abstract classes are often used to represent abstract concepts or entities.

An abstract class cannot be instantiated itself; it must be inherited. Some or all members of the class might be unimplemented, and it is up to the inheriting class to provide that implementation. Members that are implemented might still be overridden, and the inheriting class can still implement additional interfaces or other functionality.

For more information, see MustInherit.

In Visual Basic 6.0, excluding the Set method in a Property procedure creates a read-only property.

In Visual Basic 2008, you must both apply the ReadOnly keyword to a property and exclude the property Set clause to make a property read-only.

You can also apply the ReadOnly keyword to a field to make it read-only.

For more information, see ReadOnly (Visual Basic).

In Visual Basic 6.0, reflection is not supported.

In Visual Basic 2008, the classes in the .NET Framework class library System.Reflection namespace can be used to obtain information about types such as classes, interfaces, and value types at run time and to create type instances to invoke and access them.

For more information, see Reflection Namespaces in Visual Studio.

In Visual Basic 2008, the new Shared keyword can be applied to Property, Sub, Dim, Function, Operator, and Event statements to share them with all instances of a class or structure. The shared members of a class can be accessed without instantiating the class.

You can access shared members through a class's name rather than through an instance of the class.

For more information, see Shared Members in Visual Basic.

In Visual Basic 2008, programs consist of one or more source files. Source files are stored in a special type of file that has a .vb file extension. Type statements for types such as classes, structures, interfaces, enumerations, and delegates, as well as statements for standard modules, are stored in program source-code files. Visual Basic does not limit a source file to defining only one type nor does it require the name of the source file to match a type defined in the source file.

Partial-type statements allow a class, or a structure, to be defined across multiple source files. When a multi-source-file program is compiled, all of the source files are processed together as if all the source files were concatenated into one file before being processed.

For more information, see Structure of a Visual Basic Program or Partial (Visual Basic).

In Visual Basic 6.0, the Type statement is used to combine variables of several different types into one user defined type or UDT.

In Visual Basic 2008, the Structure and Enum statements are used to define user defined value types.

Use the Class, Interface, and Delegate statements to define user defined reference types.

For more information, see Structures: Your Own Data Types.

In Visual Basic 6.0, you can declare variables of different types in the same statement. If you do not specify the data type of a variable in the statement, it defaults to Variant.

In Visual Basic 2008, you can declare multiple variables of the same data type without having to specify the data type of each variable in the statement.

For more information, see Variable Declaration in Visual Basic.

When a Visual Basic 6.0 application is upgraded to Visual Basic 2008, object-oriented code constructs are converted to their nearest Visual Basic 2008 equivalent in the most expedient manner. The resulting code should not be taken as an example of how to do object-oriented programming in Visual Basic 2008.

If you intend to extend an upgraded application to use Visual Basic 2008 object-oriented constructs, you may first want to modify the upgraded code. The following pages should help you get started.

Classes for Visual Basic 6.0 Users

Classes are the building blocks of an object-oriented application.

How to: Convert a Custom Collection to a Visual Basic Typed Collection

Visual Basic 2008 provides several options for creating typed classes from Visual Basic 6.0 custom collections.

How to: Convert a User-Defined Type to a Visual Basic Structure

The Type statement of Visual Basic 6.0 is upgraded to the Structure statement of Visual Basic 2008.

How to: Convert Implements Code to a New Form of Inheritance

Newer versions of Visual Basic offer two types of inheritance, using the Implements statement and the Inherits statement.

Show:
© 2016 Microsoft