Using COM Object for inheritance (zz)

Source: Internet
Author: User
Http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/vbcn7/html/vaconhowvbinteractswithcomforinheritedobjects. asp

FromPublicThe Visual Basic. Net class is generated in the class, even those created by earlier Visual Basic versions. You can override or reload the attributes and methods of classes inherited from COM objects in the same way as override or reload the attributes and methods of any other base classes. When you have an existing class library that you do not want to re-compile, it is useful to inherit from the COM object.

The following procedure describes how to use Visual Basic 6.0 to create a COM object containing the class and use it as the base class in Visual Basic. net.

COM Object used for cost-effective drills

  1. In Visual Basic 6.0, open a new ActiveX dll project. NameProject1The project namedClass1.
  2. In "project Explorer", right-click "project1" and click "project1 properties ". The "Project Properties" dialog box is displayed.
  3. On the "General" tab of the "Project Properties" dialog box, enterComObject1You can change the project name.
  4. In "Project Resource Manager", right-clickClass1And then click Properties ". The "properties" Window of the class is displayed.
  5. Change the "name" attributeMathFunctions.
  6. In "Project Resource Manager", right-clickMathFunctionsAnd then click View code ". The code editor is displayed.
  7. Add a local variable to retain the property value:
    ' Local variable to hold property valuePrivate mvarProp1 As Integer
  8. Add PropertyLetAnd PropertyGetAttribute process:
    Public Property Let Prop1(ByVal vData As Integer)'Used when assigning a value to the property.    mvarProp1 = vDataEnd PropertyPublic Property Get Prop1() As Integer'Used when retrieving a property's value.    Prop1 = mvarProp1End Property
  9. Add a function:
    Function AddNumbers(SomeNumber As Integer, _                    AnotherNumber As Integer) As Integer   AddNumbers = SomeNumber + AnotherNumberEnd Function
  10. You can create and register a COM object by selecting "generate comobject1.dll" from the "file" menu.
    Note:You can also use Visual Basic. Net to create a COM object. For more information, see COM interoperability in Visual Basic and Visual C.

InterOP assembly

In the following process, an InterOP assembly will be created as a bridge between unmanaged code (such as a COM Object) and managed code used by Visual Studio. NET. The interoperability Assembly created by Visual Basic Processes many details of COM objects, such as "Interoperability encapsulation processing ", it is the process of packaging parameters and return values into or out of COM objects as equivalent data types. References in the Visual Basic. NET application point to the InterOP Assembly, rather than the actual COM object.

Use COM Object in Visual Basic. net

  1. Open a new visual basic. Net "Windows application" project.
  2. Click Add reference on the project menu ".

    The "add reference" dialog box is displayed.

  3. On the "com" tab, double-clickComObject1And then click OK ".
  4. Click Add new item on the project menu ".

    The "Add new item" dialog box is displayed.

  5. In the template pane, click class ".

    Default file nameClass1.vbIt will appear in the "name" field. Change this field to mathclass. VB and click open ". This will createMathClassAnd display its code.

  6. Add the following codeMathClassTo inherit 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. ForwardMathClassAdd the following code to reload and extend the inherited members:
    '  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 basic class attributes of the COM Object, reloads a method, and defines a new method to extend the class.

Class to be tested

  1. Add a button to the startup form, and double-click the button to view its code.
  2. InClickAdd the following code to the event handler process to create an instance of mathclass and call the overloaded method:
       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 " & _           MathObject.Prop1)
  3. Press F5 to run the project.

When you click the button on the form, use the data typeShortNumber of callsAddNumbersAnd Visual Basic. Net selects an appropriate method from the base class. Second callAddNumbersTargetedMathClass. The third call callsSubtractNumbersMethod. In this way, the attribute in the base class is set and its value is displayed.

Differences in Data Types

You may have noticed thatAddNumbersFunctions have the same data type as methods inherited from the base class of the COM object. This is because in Visual Basic 6.0, the parameter of the base class method is defined as a 16-bit integer, but in Visual Basic. netShortA 16-digit integer of the type is public. The new function accepts 32-bit integers and reloads the base-class functions.

When processing COM objects, it is important to verify the parameter size and data type. For example, you cannot provide a visual basic. Net set when using a COM object that accepts the Visual Basic 6.0 collection object as a parameter. For more information about data type changes, see language changes in Visual Basic.

Override the attributes and methods inherited from the com class

You can override the attributes and methods inherited from the com class, which means that you can declare local attributes or methods to replace the attributes and methods inherited from the com base class. Except for the following differences, the rules for rewriting inherited COM attributes are similar to those for rewriting other attributes and methods:

  • To override any attributes or methods inherited from the com class, you must override all other inherited attributes and methods.
  • You cannot override the attributes that use the byref parameter.

Refer:

Http://www.cnblogs.com/tonyjoule/archive/2004/05/14/9502.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.