Inheritance of components
The previous implementation of combining C # components into the VB project is just a test. Now we can implement object-oriented programming (OOP) in hybrid languages. First, let's try the inheritance of the hybrid language component.
Create a VB class library project VBComponent, and add CSharpClass in the project according to the above method. dll component reference, and then Add a new class in the component: ExtendsFromCSharp, which inherits from the C # class CSharpClass. ExtendsFromCSharp class provides an Add (x, y) method to Add the two numbers, use UML to represent Figure 4:
Figure 4 VB class inherited from Class C #
The code is as follows:
Imports CSharpClassNameSpace. CSharpClass
'Inherit from class CSharpClass in C # component
Public Class ExtendsFromCSharp
Inherits CSharpClassNameSpace. CSharpClass
Public Function Add (ByVal x As Integer, ByVal y As Integer) As Long
Return x + y
End Function
End Class
Compile and generate the VBComponent. DLL component (as for how to use and test this class, I don't need to say much about it ?).
Now, the ExtendsFromCSharp class in the VBComponent component has two methods: SaySomething comes from the C # base class CSharpClass, while the Add () function is implemented by VB. The ExtendsFromCSharp class object you created combines the functions developed in two different languages. You don't even know which language these methods and functions are developed. Is it amazing?
Let's take a short rest and make a summary:
Now we have mastered how to generate new classes from ready-made components. That is to say, we can build our own component library and dynamically expand it when appropriate, this extension is cross-language.
When using a hybrid language component in VS. NET, when the component used is modified, pay attention to the need to dynamically update the project reference. The method is to regenerate the solution, or delete the original reference and then re-join the project.
Because components inherit from each other, this causes dependencies between component libraries (such as VBComponent. the DLL component depends on CSharpClass. DLL). Therefore, in actual project development, any component library developed by a project team member must declare its component library dependency in the document. In addition, when the system is developed and deployed on the user's computer, the dependent component libraries must be bundled together and copied to the user's computer.
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