Changes to Classes and Interfaces


Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Appendix A.  VB6 Programming Element Changes in VB .NET

VB6 used the Initialize and Terminate methods that are implemented as Class_Initialize and Class_Terminate in VB6. The VB6 Initialize method has been replaced with the constructor Sub New in Visual Basic .NET.

Object destruction is indeterminate in Visual Basic .NET. Objects are destroyed whenever the Garbage Collector (GC) gets around to it. By convention you can implement a Public Dispose subroutine that releases limited resources, like file handles and record sets, and you can overload a Protected Finalize destructor. Just keep in mind that object destruction does not necessarily happen immediately. (Read Chapter 7 for more information on constructors and destructors.)

Parameterized Constructors

Visual Basic .NET supports parameterless and parameterized constructors. When you needed initial values for objects in VB6, you might have implemented an Initialize subroutine. Visual Basic .NET supports parameterized constructors, which means you can pass initial values when you create objects.

 Dim TextFile As _   New System.IO.FileStream("c:\ temp\ test.txt", IO.FileMode.Create) 

The statement demonstrates constructing a FileStream object in the System.IO namespace. The constructor parameters are the filename and mode. (Refer to Chapter 7 for more on constructors and parameterized constructors.)

Option Private Module Is Not Supported

Visual Basic 6 supported the Option Private Module statement, which makes all of the members in a module private. Use the access modifier Private on every member in the module to create the same result in Visual Basic .NET. Option Private Module is not supported in Visual Basic .NET.

Changes to Interfaces

Every VB6 class is a COM class. When you define a class in VB6, all you have to do to implement the public methods in the class is to add a statement Implements classname in a second module. The result is that the second module becomes an implementation of the first module. The first module is the interface, and the second module is the implementation.

When you add the Implements statement to a module, you can select the interface name from the Objects list in the code view and select each of the public interface methods from the procedures list. VB6 generates the interface method automatically.


You can select an interface from the Class Name list and the interface method from the Method Name list (in the code editor), and Visual Basic .NET will generate the interface method body automatically, too.

Visual Basic .NET requires that you add the Implements statement to the class definition, and an Implements clause is added as a suffix to the end of the procedure header. (Chapter 7 discusses the grammar for implementing interfaces in Visual Basic .NET.) Listing A.4, excerpted from Chapter 7, demonstrates an example of an implementation of the IDisposable.Dispose interface.

Listing A.4 An excerpt from Chapter 7, demonstrating how to implement interface methods in Visual Basic .NET
 Public Sub Dispose() Implements IDisposable.Dispose   If (FDisposed) Then Exit Sub   FDisposed = True   Close()   FArrayList = Nothing End Sub 

The subroutine is a member of a class that implements IDisposable. VB6 (as shown in Listing A.5) would generate a private subroutine prefixed with the interface name.

Listing A.5 VB6 generated a private method with the interface name prefixed to the procedure name
 Private Sub IDisposable_Dispose()   If (FDisposed) Then Exit Sub   FDisposed = True   Close()   FArrayList = Nothing End Sub 

The biggest difference between the VB6 and Visual Basic .NET interface methods is the syntax for indicating that a method or property implements an interface.


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
Year: 2001
Pages: 222 © 2008-2017.
If you may any questions please contact us: