VB.NET creates managed code – code that runs within the .NET Framework as discussed in Chapter 2. All managed code can interact with other managed code, regardless of the original language used to create those components. This means that we can create a class in one language and make use of it in another – in any way, including through inheritance.
In fact, we do this all the time. Much of the .NET system class library is written in C#, but we interact with and even inherit from those classes on a regular basis as we program in VB.NET.
Creating the VB.NET Base Class
For
instance, we can create a Class Library project in VB.NET named vblib
and add a simple class named Parent
such as:
Public
Class Parent
Public Sub DoSomething()
MsgBox(“Parent DoSomething”, MsgBoxStyle.Information, “Parent”)
End Sub
End
Class
This will act as the base class from which we’ll create a subclass in C#.
Creating the C# Subclass
We
can then add a new C# Class Library project to the
solution (using File
| Add Project) and name it cslib
.
Add a reference to our vblib
project by using the Project
| Add Reference menu option.
While we are referencing this
project directly within the IDE, we wouldn’t need the VB.NET source code. Instead,
we could have built the vblib
project, thus creating an assembly, and then referenced that assembly from within
the C# project to gain access to the base class.
In the Class1.cs
file change the code to appear as follows:
namespace
cslib
{
using System.WinForms;
using vblib;
public class csclass : Parent
{
public csclass()
{
Messagebox.Show(“csclass constructor”);
}
}
}
This C# code shares common concepts with
the VB.NET code we’ve seen so far in the book. However, C# is largely derived
from C and C++ language syntax so things are a bit different. All lines of code
must end with a semicolon to denote the end of the statement. Also, left and
right brackets are used to form block structures. In VB.NET we might have a
Sub...End
Sub
block, while in C# we’ll have {
and }
.
Let’s walk through it to make everything clear. The first line defines the namespace for the file. In C# all namespaces are explicitly declared in each code module:
namespace cslib
In C# the using
keyword is equivalent to the Imports
keyword in VB.NET. Since we’re using both the Systems.WinForms
namespace and the namespace from our vblib
project, we have using
statements to make those namespaces easy to use:
using System.WinForms;
using vblib;
The next line of code declares the class
we’re creating and indicates that it is a subclass of Parent
:
public class csclass : Parent
In C# a subclass is declared by declaring a class, followed by a colon and then the name of the base class. This is equivalent to the following VB.NET code:
Public
Class csclass
Inherits Parent
In VB.NET constructor methods are created
using the reserved method name New
.
In C#, constructors are created by using the name of the class itself as the
method name:
public csclass()
{
Messagebox.Show(“csclass constructor”);
}
The brackets ({
and }
)
form a block structure within which we place the code for the method. In this
case the method simply displays a dialog box indicating that the constructor
method was invoked.
Now we can create client code to work with this new object.
Creating a Client Application
Use File
| Add Project to add a new VB.NET Windows Application
project to the solution. In this new project add a reference to the cslib
project by using the Project
| Add Reference menu option. Right-click on the project
and choose the Set
As Startup Project option so this project will be
run when we press F5.
Notice that we have no reference to the
vblib
project at all. That is fine, since we aren’t directly using any code from that
assembly. All our client application cares about is the cslib
project.
While we are referencing the cslib
project directly within the IDE, we wouldn’t need the C# source code.
Instead, we could have built the cslib
project, thus creating an assembly, and then referenced that assembly from within
the client project to gain access to our test C# class.
Now add a button to Form1
and write the following code behind that button:
Protected
Sub Button1_Click(ByVal
sender As Object, _
ByVal e As System.EventArgs)
Dim obj As New cslib.csclass()
obj.DoSomething()
End
Sub
This is really no different than if we’d created a VB.NET subclass – but in this case our subclass is actually written in a different language.
When we run this application and click
the button, we should see a dialog box with a message indicating the constructor
from the csclass
was called, then another dialog indicating that the DoSomething
method from our VB.NET base class was called.
Comments