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
- In Visual Basic 6.0, open a new ActiveX dll project. NameProject1The project named
Class1
.
- In "project Explorer", right-click "project1" and click "project1 properties ". The "Project Properties" dialog box is displayed.
- On the "General" tab of the "Project Properties" dialog box, enter
ComObject1
You can change the project name.
- In "Project Resource Manager", right-click
Class1
And then click Properties ". The "properties" Window of the class is displayed.
- Change the "name" attribute
MathFunctions
.
- In "Project Resource Manager", right-click
MathFunctions
And then click View code ". The code editor is displayed.
- Add a local variable to retain the property value:
' Local variable to hold property valuePrivate mvarProp1 As Integer
- 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
- Add a function:
Function AddNumbers(SomeNumber As Integer, _ AnotherNumber As Integer) As Integer AddNumbers = SomeNumber + AnotherNumberEnd Function
- 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
- Open a new visual basic. Net "Windows application" project.
- Click Add reference on the project menu ".
The "add reference" dialog box is displayed.
- On the "com" tab, double-click
ComObject1
And then click OK ".
- Click Add new item on the project menu ".
The "Add new item" dialog box is displayed.
- In the template pane, click class ".
Default file nameClass1.vb
It will appear in the "name" field. Change this field to mathclass. VB and click open ". This will createMathClass
And display its code.
- Add the following code
MathClass
To 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
- Forward
MathClass
Add 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
- Add a button to the startup form, and double-click the button to view its code.
- 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)
- Press F5 to run the project.
When you click the button on the form, use the data typeShortNumber of callsAddNumbers
And Visual Basic. Net selects an appropriate method from the base class. Second callAddNumbers
TargetedMathClass
. The third call callsSubtractNumbers
Method. In this way, the attribute in the base class is set and its value is displayed.
Differences in Data Types
You may have noticed thatAddNumbers
Functions 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