This documentation is archived and is not being maintained.

Walkthrough: Implementing Inheritance with COM Objects

You can derive Visual Basic .NET 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 .NET.

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(SomeNumber As Integer, _
                        AnotherNumber As Integer) As Integer
       AddNumbers = SomeNumber + AnotherNumber
    End Function
  10. Create and register the COM object by selecting Make ComObject1.dll from the File menu.
    Note   You can also create COM objects using Visual Basic .NET. For details, see COM Interoperability in Visual Basic and Visual C#.

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 .NET 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 .NET application points to the interop assembly, not the actual COM object.

To use a COM object with Visual Basic .NET

  1. Open a new Visual Basic .NET 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 Open. This will create a class named MathClass, and display 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 and extend the inherited members by adding the following code to MathClass:
    '  This method overloads the method AddNumbers from the base class.
       Overloads Function AddNumbers(ByRef SomeNumber As Integer, _
                                     ByRef AnotherNumber As Integer _
                                     ) As Integer
          AddNumbers = SomeNumber + AnotherNumber
       End Function
    '  The following function extends the inherited class.
       Function SubtractNumbers(ByVal SomeNumber As Integer, _
                                 ByVal AnotherNumber As Integer) As Integer
          SubtractNumbers = 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 Integer
       Dim Result2 As Short
       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 " & _
  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 .NET 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.

Data Type Differences

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 .NET. 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 .NET collection. For a more information about data type changes, see Language Changes in Visual Basic.

Overriding Properties and Methods Inherited from COM classes

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

COM Interoperability in Visual Basic and Visual C# | Language Changes in Visual Basic | Using Inherits to Create a Derived Class | Inherits Statement | Overriding Properties and Methods | Short Data Type