逐步解說:實作 COM 物件的繼承 (Visual Basic)

您可以從 COM 物件中的 Public 類別衍生 Visual Basic 類別,甚至是在舊版 Visual Basic 中建立的類別。 可以覆寫或多載繼承自 COM 物件的類別屬性和方法,就如同可以覆寫或多載任何其他基底類別的屬性和方法一樣。 當您有不想重新編譯的現有類別庫時,從 COM 物件的繼承很有幫助。

下列程序示範如何使用 Visual Basic 6.0 建立包含類別的 COM 物件,然後將其當做基底類別使用。

注意

在下列指示的某些 Visual Studio 使用者介面項目中,您的電腦可能會顯示不同的名稱或位置: 您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。 如需詳細資訊,請參閱將 Visual Studio IDE 個人化

若要建置本逐步解說中使用的 COM 物件

  1. 在 Visual Basic 6.0 中,開啟新的 ActiveX DLL 專案。 系統會建立名為 Project1 的專案。 該專案有名為 Class1 的類別。

  2. 在 [專案總管]中,以滑鼠右鍵按一下 [Project1],然後按一下 [Project1 屬性]。 [專案屬性] 對話方塊隨即顯示。

  3. 在 [專案屬性] 對話方塊的 [一般] 索引標籤上,於 [專案名稱] 欄位輸入 ComObject1 來變更專案名稱。

  4. 在 [專案總管] 中,以滑鼠右鍵按一下 Class1,然後按一下 [屬性]。 類別的 [屬性] 視窗隨即顯示。

  5. Name 屬性變更為 MathFunctions

  6. 在 [專案總管] 中,以滑鼠右鍵按一下 MathFunctions,然後按一下 [檢視程式碼]。 [程式碼編輯器] 隨即顯示。

  7. 新增區域變數以保存屬性值:

    ' Local variable to hold property value
    Private mvarProp1 As Integer
    
  8. 新增屬性 Let 和屬性 Get 屬性程序:

    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. 新增函式:

    Function AddNumbers(
       ByVal SomeNumber As Integer,
       ByVal AnotherNumber As Integer) As Integer
    
       AddNumbers = SomeNumber + AnotherNumber
    End Function
    
  10. 按一下 [檔案] 功能表上的 [建立 ComObject1.dll],以建立和註冊 COM 物件。

    注意

    雖然您也可以將以 Visual Basic 建立的類別公開為 COM 物件,但該類別不是真正的 COM 物件,而且無法在此逐步解說中使用。 如需詳細資訊,請參閱 .NET Framework 應用程式中的 COM 互通性

Interop 組件

在下列程序中,您將建立 Interop 組件,該組件可作為 Unmanaged 程式碼 (例如 COM 物件) 和 Visual Studio 所使用 Managed 程式碼之間的橋接器。 Visual Basic 所建立的 Interop 組件會處理許多使用 COM 物件的詳細資料,例如 Interop 封送處理、封裝參數的處理序,以及將值傳回至對等的資料類型,因為這些項目在 COM 物件之間移動。 Visual Basic 應用程式中的參考會指向 Interop 組件,而不是實際的 COM 物件。

若要搭配 Visual Basic 2005 和更新版本使用 COM 物件

  1. 開啟新的 Visual Basic Windows 應用程式專案。

  2. 在 [專案] 功能表上,按一下 [加入參考]。

    [新增參考] 對話方塊隨即顯示。

  3. 在 [COM] 索引標籤上的 [元件名稱] 清單中,按兩下 ComObject1,然後按一下 [確定]

  4. 在 [專案] 功能表上,按一下 [加入新項目]

    隨即顯示 [ 新增項目] 對話方塊。

  5. 在 [範本] 窗格中,按一下 [類別]

    預設檔案名稱 Class1.vb 會顯示在 [名稱] 欄位中。 將此欄位變更為 MathClass.vb,然後按一下 [新增]。 這會建立名為 MathClass 的類別,並顯示其程式碼。

  6. 將下列程式碼新增至 MathClass 頂端,以從 COM 類別繼承。

    ' 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. 將下列程式碼新增至 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. 將下列程式碼新增至 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
    

新類別會繼承 COM 物件中基底類別的屬性、多載方法,並定義新的方法來擴充類別。

若要測試繼承的類別

  1. 將按鈕新增至您的啟動表單,然後按兩下以檢視其程式碼。

  2. 在按鈕的 Click 事件處理常式程序中,新增下列程式碼以建立 MathClass 的執行個體並呼叫多載方法:

    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. 按 F5 執行專案。

當您按一下表單上的按鈕時,會先使用 Short 資料類型號碼呼叫 AddNumbers 方法,而 Visual Basic 會從基底類別中選擇適當的方法。 AddNumbers 的第二次呼叫會從 MathClass 導向至多載方法。 第三個呼叫會呼叫 SubtractNumbers 方法,這會擴充類別。 基底類別中的屬性已設定,並會顯示值。

後續步驟

您可能已注意到,多載 AddNumbers 函式似乎與繼承自 COM 物件基底類別的方法具有相同資料類型。 這是因為基底類別方法的引數和參數在 Visual Basic 6.0 中定義為 16 位整數,但會在較新版本的 Visual Basic 中公開為類型 Short 的 16 位整數。 新的函式接受 32 位整數,並多載基底類別函式。

使用 COM 物件時,請務必確認參數的大小和資料類型。 例如,當您使用 COM 物件 (該物件接受 Visual Basic 6.0 集合物件做為引數) 時,便無法從較新版本的 Visual Basic 提供集合。

可以覆寫繼承自 COM 類別的屬性和方法,這表示您可以宣告本機屬性或方法,以取代繼承自基底 COM 類別的屬性或方法。 覆寫繼承 COM 屬性規則類似於覆寫其他屬性和方法的規則,但有下列例外狀況:

  • 如果您覆寫繼承自 COM 類別的任何屬性或方法,則必須覆寫所有其他繼承的屬性和方法。

  • 無法覆寫使用 ByRef 參數的屬性。

另請參閱