Dela via


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

Visual Basic 6.0 uses the Implements statement for interface inheritance. Newer versions of Visual Basic offer two types of inheritance, using the Implements Statement and the Inherits Statement. This topic demonstrates how to convert code using these two options.

The following Visual Basic 6.0 code is considered on this page:

' Account class.
Private mvarBalance As Currency
Private mvarOwner As String

Public Property Let Owner(ByVal vData As String)
    mvarOwner = vData
End Property

Public Property Get Owner() As String
    Owner = mvarOwner
End Property

Public Function Deposit(ByVal amount As Currency) As Currency
End Function

' Savings class.
Private mvarBalance As Currency
Private mvarOwner As String 'local copy

Public Property Let Owner(ByVal vData As String)
    mvarOwner = vData
End Property

Public Property Get Owner() As String
    Owner = mvarOwner
End Property

Public Function Deposit(ByVal amount As Currency) As Currency
End Function

To convert using a common interface

  1. Run the Upgrade Wizard.

    Note

    The Upgrade Wizard is a tool used to upgrade a Visual Basic 6.0 application to Visual Basic 2008. It is automatically launched when you open a Visual Basic 6.0 project in Visual Basic 2008. For more information, see How to: Upgrade a Project with the Visual Basic Upgrade Wizard.

    The wizard will convert the Account class to an interface (_Account), and create two classes that implement the interface (Account and Savings.)

    Interface _Account
        Property Owner() AsStringFunction Deposit(ByVal amount AsDecimal) AsDecimalEndInterfaceFriendClass Account
        Implements _Account
        Private mvarBalance AsDecimalPrivate mvarOwner AsStringPublicProperty Owner() AsStringImplements _Account.Owner
            Get
                Owner = mvarOwner
            EndGetSet(ByVal Value AsString)
                mvarOwner = Value
            EndSetEndPropertyPublicFunction Deposit(ByVal amount AsDecimal) _
            AsDecimalImplements _Account.Deposit
        EndFunctionEndClassFriendClass Savings
        Implements _Account
    
        PrivateFunction Account_Deposit(ByVal amount AsDecimal) _
            AsDecimalImplements _Account.Deposit
        EndFunctionPrivateProperty Account_Owner() AsString _
            Implements _Account.Owner
            GetEndGetSet(ByVal Value AsString)
            EndSetEndPropertyPublicFunction AddInterest() AsObjectEndFunctionEndClass
    
  2. The code to create and use an instance of the Savings account could look like this.

    Dim savings As _Account
    savings = New Savings
    savings.Owner = "Jeff Pike"
    

If you want Account to be the base class, you can modify the wizard output.

To convert with Interface and Implements

  1. Run the Upgrade Wizard.

  2. Delete the Account class.

  3. Rename the _Account class to Account.

  4. In the Savings class, replace _Account with Account.

    FriendClass Savings
        Implements Account
    
        PrivateFunction Account_Deposit(ByVal amount AsDecimal) _
            AsDecimalImplements Account.Deposit
        EndFunctionPrivateProperty Account_Owner() AsStringImplements Account.Owner
            GetEndGetSet(ByVal Value AsString)
            EndSetEndPropertyPublicFunction AddInterest() AsObjectEndFunctionEndClass
    
  5. The code to create and use an instance of the Savings account could look like this.

    Dim savings As Account
    savings = New Savings
    savings.Owner = "Jeff Pike"
    

If you want the Account class to be a base class, you can modify the wizard output.

To convert with Class and Inherits

  1. Run the Upgrade Wizard.

  2. Delete the _Account interface.

  3. In the Account class, remove references to the _Account in Implements statements.

    FriendClass Account
        Private mvarBalance AsDecimalPrivate mvarOwner AsStringPublicProperty Owner() AsStringGet
                Owner = mvarOwner
            EndGetSet(ByVal Value AsString)
                mvarOwner = Value
            EndSetEndPropertyPublicFunction Deposit(ByVal amount AsDecimal) AsDecimalEndFunctionEndClass
    
  4. In the Savings class, replace Implements _Account with Inherits Account.

  5. In the Savings class, remove the Owner and Deposit members.

    FriendClass Savings
        Inherits Account
    
        PublicFunction AddInterest() AsObjectEndFunctionEndClass
    
  6. The code to create and use an instance of the Savings account could look like this.

    Dim savings As Account
    savings = New Savings
    savings.Owner = "Jeff Pike"
    

See Also

Tasks

How to: Upgrade a Project with the Visual Basic Upgrade Wizard

Reference

Class Statement (Visual Basic)

Inherits Statement

Interface Statement (Visual Basic)

Implements Statement

Other Resources

Inheritance in Visual Basic

Interfaces in Visual Basic