Based on the component. NET software Development (2)

Source: Internet
Author: User
Tags implement inheritance integer mixed reference
Inheritance of components
The previous implementation of the C # component into the VB project is only his experiment, now we implement mixed-language object-oriented programming (OOP), first of all, let's try the hybrid language component inheritance.

Create a new VB Class Library engineering VBComponent, In the project, add a reference to the CSharpClass.dll component in the above method, and then add a new class to the component: Extendsfromcsharp, which inherits from the C # class Csharpclass,extendsfromcsharp class provides an add (x , Y) method, adds two numbers, which are represented in UML as Figure 4:



Figure 4 VB classes inherited from C # classes

The specific code is as follows:



Imports Csharpclassnamespace.csharpclass

' Inherit from class Csharpclass in C # components

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 the Build VBComponent.DLL component (as for how to use and test this class, don't I have to say more?) )。

The Extendsfromcsharp class in the VBComponent component now has two methods: SaySomething from the C # base class Csharpclass, and the Add () function is implemented by VB. Is it magical that you create Extendsfromcsharp objects that combine the capabilities of two different languages, and you don't even know which language the methods and functions were developed by?

Let's take a little break and make a summary:

Now we have a way of deriving new classes from off-the-shelf components, which means that we can build our own component libraries and expand them dynamically at the right time, and this extension is cross-language.

When you use a mixed language component in vs.net, note that you need to update the project references dynamically when the component you are using is modified. The method is to regenerate the solution, or delete the original reference and rejoin the project.

Because of the inheritance between the components, This creates dependencies between component libraries (such as the VBComponent.DLL component depends on CSharpClass.DLL), so that in actual project development, the component libraries developed by any team member require that their component library dependencies be declared in the document. In addition, when system development completes deployment to a user's computer, the interdependent component libraries must be tied together and copied to the user's computer, and cannot be supplied with only one.



Below, let's go a little farther and implement polymorphism between components!

Polymorphic calls between components
We're going to implement a "classic" polymorphic example that illustrates the fact that circles and rectangles are geometries and can be used to find areas.

To continue with the example above, select the original CSharpClass.cs file in the C # project, and in addition to the class Csharpclass, enter the following code:



public interface Imyshape

{

Double area ();

}

public class Csharprect:imyshape

{

public float width;

public float height;



Public double Area ()

{

return width*height;

}

}



Obviously, here we have defined an interface Imyshape, the class Csharprect implements this interface, and it provides its own implementation for the function of the area defined by the interface double ():

Rectangular area = length * Width

Compile the build solution and CSharpClass.DLL get updated.

Open the VBComponent project and notice that refreshing the project's reference to CSharpClass.DLl is in the Select menu: View à object Browser to see if Csharpclass reflects the new changes, as shown in the 5 figure:



Figure 5 Checking to see if a piece is updated

Create a new class file Vbcircle.vb and enter the following code:



Public Class Vbcircle

Implements Csharpclassnamespace.imyshape



Private _radius as Single



Property Radius () as single

Get

Radius = _radius

End Get

Set (ByVal Value as single)

_radius = Value

End Set

End Property



Public Sub New ()

_radius = 0

End Sub



Public Function area () as Double Implements CSharpClassNameSpace.IMyShape.Area

return System.Math.PI * _radius * _radius

End Function

End Class



Compile, generate VBComponent.DLL.

Now we have implemented the following object-oriented system design based on components:






Create a new Windows application using VB, add a reference to the above two component libraries in the project, add a button to the form, and write the following code in its Click event:



Private Sub Button_Click (...) Handles Button6.click

Dim Vbobj as New vbcomponent.vbcircle ()

Vbobj.radius =10

Dim Csharpobj as New csharpclassnamespace.csharprect ()

Dim area as Double

Csharpobj.width = 10

Csharpobj.height = 20

Calarea (Vbobj)

Calarea (Csharpobj)

End Sub



where Calarea () is a sub process, it receives a imyshape type parameter, can pass to it any one implements the Imyshape interface the object, this kind of interface rather than the concrete class to code the method, is the typical polymorphic coding way.



Private Sub Calarea (ByVal obj as Csharpclassnamespace.imyshape)

MessageBox.Show (convert.tostring) (obj. Area ())

End Sub



After testing, cross-language, cross-component polymorphism can indeed act normally.



Well, now let's think about what has been achieved and how it can be applied to development practices:

We can encapsulate an interface into a component, and then the classes that implement this interface can be put into different components and given to different programmers to develop in their favorite language. Programmers who use these components, as long as they always use interface variables instead of specific class variables in his code, we can dynamically replace the components of a specific implementation interface without affecting the programs that use those components.

Wow, this does not mean that we can take a complete software system to unload eight pieces, and then choose a component to insert the system according to the specific situation? That's great! Next, let's begin the most exciting journey of component programming---the dynamic assembly and plug of software components.



Related Article

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.