Compartilhar via


Walkthrough: Implementing Inheritance with COM Objects 

You can derive Visual Basic 2005 classes from Public classes in COM objects, even those created with previous versions of Visual Basic. The properties and methods of classes inherited from COM objects can be overridden or overloaded in the same way that properties and methods of any other base class can be overridden or overloaded. Inheritance from COM objects is useful when you have an existing class library that you do not want to recompile.

The following procedure shows how to use Visual Basic 6.0 to create a COM object containing a class, and then use it as a base class in Visual Basic 2005.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To build the COM object used in this walkthrough

  1. In Visual Basic 6.0, open a new ActiveX DLL project. A project named Project1 is created with a class named Class1.

  2. In the Project Explorer, right-click Project1, and then click Project1 Properties. The Project Properties dialog box is displayed.

  3. On the General tab of the Project Properties dialog box, change the project name by typing ComObject1 in the Project Name field.

  4. In the Project Explorer, right-click Class1, and then click Properties. The Properties window for the class is displayed.

  5. Change the Name property to MathFunctions.

  6. In the Project Explorer, right-click MathFunctions, and then click View Code. The Code Editor is displayed.

  7. Add a local variable to hold the property value:

    ' Local variable to hold property value
    Private mvarProp1 As Integer
    
  8. Add Property Let and Property Get property procedures:

    Public Property Let Prop1(ByVal vData As Integer)
       'Used when assigning a value to the property.
       mvarProp1 = vData
    End Property
    Public Property Get Prop1() As Integer
       'Used when retrieving a property's value.
       Prop1 = mvarProp1
    End Property
    
  9. Add a function:

    Function AddNumbers( _
       ByVal SomeNumber As Integer, _
       ByVal AnotherNumber As Integer) _
       As Integer
    
       AddNumbers = SomeNumber + AnotherNumber
    End Function
    
  10. Create and register the COM object by choosing Make ComObject1.dll from the File menu.

    Note

    Although you can also expose a class created with Visual Basic as a COM object, it is not a true COM object and cannot be used in this walkthrough. For details, see COM Interoperability in .NET Framework Applications.

Interop Assemblies

In the following procedure, you will create an interop assembly, which acts as a bridge between unmanaged code (such as a COM object) and the managed code Visual Studio uses. The interop assembly that Visual Basic creates handles many of the details of working with COM objects, such as interop marshaling, the process of packaging parameters and return values into equivalent data types as they move to and from COM objects. The reference in the Visual Basic application points to the interop assembly, not the actual COM object.

To use a COM object with Visual Basic 2005

  1. Open a new Visual Basic Windows Application project.

  2. On the Project menu, click Add Reference.

    The Add Reference dialog box is displayed.

  3. On the COM tab, double click ComObject1 in the Component Name list and click OK.

  4. On the Project menu, click Add New Item.

    The Add New Item dialog box is displayed.

  5. In the Templates pane, click Class.

    The default file name, Class1.vb, appears in the Name field. Change this field to MathClass.vb and click Add. This creates a class named MathClass, and displays its code.

  6. Add the following code to the top of MathClass to inherit from the COM class.

    ' The inherited class is called MathFunctions in the base class,
    ' but the interop assembly appends the word Class to the name.
    Inherits ComObject1.MathFunctionsClass
    
  7. Overload the public method of the base class by adding the following code to MathClass:

    '  This method overloads the method AddNumbers from the base class.
    Overloads Function AddNumbers( _
        ByVal SomeNumber As Integer, _
        ByVal AnotherNumber As Integer) _
        As Integer
    
        Return SomeNumber + AnotherNumber
    End Function
    
  8. Extend the inherited class by adding the following code to MathClass:

    '  The following function extends the inherited class.
    Function SubtractNumbers( _
        ByVal SomeNumber As Integer, _
        ByVal AnotherNumber As Integer) _
        As Integer
    
        Return AnotherNumber - SomeNumber
    End Function
    

The new class inherits the properties of the base class in the COM object, overloads a method, and defines a new method to extend the class.

To test the inherited class

  1. Add a button to your startup form, and then double-click it to view its code.

  2. In the button's Click event handler procedure, add the following code to create an instance of MathClass and call the overloaded methods:

    Dim Result1 As Short
    Dim Result2 As Integer
    Dim Result3 As Integer
    Dim MathObject As New MathClass
    Result1 = MathObject.AddNumbers(4S, 2S) ' Add two Shorts.
    Result2 = MathObject.AddNumbers(4, 2) 'Add two Integers.
    Result3 = MathObject.SubtractNumbers(2, 4) ' Subtract 2 from 4.
    MathObject.Prop1 = 6 ' Set an inherited property.
    
    MsgBox("Calling the AddNumbers method in the base class " & _
           "using Short type numbers 4 and 2 = " & Result1)
    MsgBox("Calling the overloaded AddNumbers method using " & _
           "Integer type numbers 4 and 2 = " & Result2)
    MsgBox("Calling the SubtractNumbers method " & _
           "subtracting 2 from 4 = " & Result3)
    MsgBox("The value of the inherited property is " & _
            MathObject.Prop1)
    
  3. Run the project by pressing F5.

When you click the button on the form, the AddNumbers method is first called with Short data type numbers, and Visual Basic chooses the appropriate method from the base class. The second call to AddNumbers is directed to the overload method from MathClass. The third call calls the SubtractNumbers method, which extends the class. The property in the base class is set, and the value is displayed.

Next Steps

You may have noticed that the overloaded AddNumbers function appears to have the same data type as the method inherited from the COM object's base class. This is because the base class method's arguments and parameters are defined as 16-bit integers in Visual Basic 6.0, but they are exposed as 16-bit integers of type Short in Visual Basic 2005. The new function accepts 32-bit integers, and overloads the base class function.

When working with COM objects, it is important to verify the size and data types of parameters. For example, when using a COM object that accepts a Visual Basic 6.0 collection object as an argument, you cannot provide a Visual Basic 2005 collection. For more information about data type changes, see Language Changes for Visual Basic 6.0 Users.

Properties and methods inherited from COM classes can be overridden, meaning that you can declare a local property or method that replaces a property or method inherited from a base COM class. The rules for overriding inherited COM properties are similar to the rules for overriding other properties and methods with the following exceptions:

  • If you override any property or method inherited from a COM class, you must override all the other inherited properties and methods.

  • Properties that use ByRef parameters cannot be overridden.

See Also

Tasks

How to: Create Derived Classes

Reference

Inherits Statement
Short Data Type (Visual Basic)

Concepts

Overriding Properties and Methods

Other Resources

COM Interoperability in .NET Framework Applications
Language Changes for Visual Basic 6.0 Users