Cross inheritance of C # and VB

Source: Internet
Author: User
Tags constructor inheritance mixed

Vb. NET can create processing code that is running in the. NET Framework. All management code can interact with other types of processing code, regardless of the programming language in which we create these components. This means that we can create a class on one programming language and then apply it to another programming language, including, of course, inheritance. The hybrid programming mechanism of the formal cross language mixed inheritance.

In fact, there are many programs involved that are already applying the technology. Many of them. NET System class libraries are written on C #, and when we write programs on vb.net, these classes can be inherited as base classes.

Create vb.net base class

For example, we can create a class library project in vb.net named Vblib and add a simple class to parent, code as follows:

1Public Class ParentClass Parent
2  Public Sub DoSomething()Sub DoSomething()
3   MsgBox(Parent DoSomething, MsgBoxStyle.Information, Parent)
4  End Sub
5End Class

With this base class, we can create a subclass on C #.

To create a C # subclass

We can add a new class library project by using the menu file->addproject (add Engineering) and naming it cslib. Then add a reference to the Vblib project by selecting the menu project (engineering)->addreference (add Reference).

When we reference this project directly in the IDE, we do not need vb.net source code. Instead, we can create a vblib project, create a component, and then reference this component from the C # project to get access to the base class for the following code:

1namespace cslib
2{
3  using System.WinForms;
4  using vblib;
5
6  public class csclass : Parent
7  {
8   public csclass()
9   {
10     Messagebox.Show(csclass constructor);
11   }
12  }
13}

The C # code above shares the code in the vb.net. However, C # 's syntax is largely from the C and C + + languages, so programming can be a bit more complicated. The end of all code statements is terminated with a semicolon (;) and a block structure is defined using the left and right brackets ({and}). and defining a block structure in vb.net is the use of sub ... Endsub statement, which is exactly the difference between vb.net and C # syntax, so be sure to note the differences in syntax when using vb.net and C # mixed programming.

Let's take a closer look at the code above. The first line of code is to define a name space (namespace) for the file. All namespaces in C # are explicitly defined in each code module:

1 Namespace cslib the Using keyword in C # is equivalent to the Imports keyword in vb.net. Because we want to use system.winforms and the namespaces from Vblib, we use the following statements to introduce these namespaces:

1 Using system.winforms;

2 Using Vblib; The next line of code is to declare the class we want to create and a subclass that indicates that the class is parent:

1 public class csclass:parent in C # A subclass is defined by defining a class with a colon (:) followed by a class name, followed by a base class. This statement is equivalent to the following vb.net code:

1 Public Class Csclass

2 Inherits Parent

Constructors are created in vb.net by using the reserved method new. In C # Constructors are created using the name of the method, such as:

1public csclass()
2{
3  Messagebox.Show(csclass constructor);
4}

In C #, curly braces ({and}) define a block structure in which we can place the code of the method. The method in this example is very simple, except that a dialog box is displayed to indicate that the constructor was invoked. So we can create the customer code for the new project.

Create a client application

You can first add a new vb.net Windows Application project by using the menu file->addproject (add Engineering). In this new project you can select the menu item Project (Project)->addreference (add Reference) to add a reference for the Cslib project. Click the right mouse button in the project and choose the Setasstartupproject option from the pop-up menu, which is designed to work when you press the F5 shortcut key.

It is noteworthy here that there is no reference to the Vblib project because we do not directly use any code from the component. All client applications are concerned with Cslib engineering.

When we refer directly to the Cslib project in the IDE, we do not need C # code. Instead, we can create a cslib project, create a component, and then refer to the component from the customer's project to access our test C # class. Then we'll add a button to the form and write the following code for the button:

1Protected Sub Button1_Click()Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
2  Dim obj As New cslib.csclass()
3  obj.DoSomething()
4End Sub 
 

Although it is no different to create a vb.net subclass, in this example we use a different programming language to implement it. The difference between the two of you to understand, I believe it will help you.

Well, when we run the application and click on the button, we can see a dialog box that shows the constructor of the csclass we called, and another dialog box that shows the DoSomething method of the VB.net base class we call.

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.