共用方式為


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

您可以將 Visual Basic 類別從 COM 物件的類別中衍生出來,包括那些在舊版 Visual Basic 中建立的類別。 繼承自 COM 物件的類別屬性和方法可以覆寫或多載,就像任何其他基類的屬性和方法可以覆寫或多載一樣。 當您有不想重新編譯的現有類別庫時,從 COM 對象的繼承很有用。

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

備註

您的電腦可能會在下列指示中顯示某些 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. 新增 Property Let 和 Property 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 組件

在下列程序中,您將建立一個互操作組件,它會充當非受控程式代碼(例如 COM 物件)與 Visual Studio 使用的受控程式代碼之間的橋樑。 Visual Basic 所建立的互操作組件會處理許多與 COM 物件互動的細節,例如互操作封送,這個過程會將參數和傳回值封裝為等同的數據類型,以便在 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 執行專案。

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

後續步驟

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

使用 COM 物件時,請確定您確認參數的大小和數據類型。 例如,當您使用接受 Visual Basic 6.0 集合物件的 COM 物件做為自變數時,您無法從較新的 Visual Basic 版本提供集合。

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

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

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

另請參閱