Classes acting as interfaces in Visual Basic 6.0 are upgraded to interfaces in Visual Basic 2008. Consider this example of a base class and derived class from Visual Basic 6.0:
' Contents of class BaseClass
Public Sub BaseMethod()
End Sub
' Contents of class DerivedClass
Implements BaseClass
Private Sub BaseClass_BaseMethod()
End Sub
The Upgrade Wizard produces this upgraded code:
Option Strict Off
Option Explicit On
Interface _BaseClass
Sub BaseMethod()
End Interface
Friend Class BaseClass
Implements _BaseClass
Public Sub BaseMethod() Implements _BaseClass.BaseMethod
End Sub
End Class
Friend Class DerivedClass
Implements _BaseClass
Private Sub BaseClass_BaseMethod() Implements _BaseClass.BaseMethod
End Sub
End Class
Using inheritance instead of interfaces, the upgraded code can be modified to:
Friend Class BaseClass
Public Sub BaseMethod()
' Add code here to define BaseMethod.
End Sub
End Class
Friend Class DerivedClass
Inherits BaseClass
End Class
This eliminates one level of indirection, _BaseClass. Also, it eliminates one method definition, BaseClass_BaseMethod, because the method is inherited from the base class and does not need to be coded again. If the programmer wanted a different behavior for the derived class, the BaseMethod can be overridden as shown:
Friend Class BaseClass
Public Overridable Sub BaseMethod()
' Add code here to define BaseMethod.
End Sub
End Class
Friend Class DerivedClass
Inherits BaseClass
Public Overrides Sub BaseMethod()
' Add code here to define behavior for DerivedClass.
End Sub
End Class
Some techniques to consider in upgraded interfaces include:
-
Replace Interface statements with Inherits statements.
-
Implement the base class as an interface, rather than as a class that implements an interface. For more information see Interface Changes for Visual Basic 6.0 Users.
-
Implement base classes as MustInherit classes.
-
Implement derived classes as NotInheritable.