HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
VB.NET PROGRAMMING PART 7 - CROSS-LANGUAGE INHERITANCE

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

VB.NET creates managed code - code that runs within the .NET Framework. 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.
Click here to be kept informed of our new Tutorials.


This free tutorial is a sample from the book VB.NET Programming with the Public Beta.


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.

Visual Inheritance

So far we've been discussing the new OO features of the VB.NET language, with a large focus on inheritance.

However, VB.NET also supports visual inheritance for Windows Forms. This means that we can create a Windows Forms form, and then inherit from that form to create other forms that have the same layout, controls, and behaviors. This topic was covered in more detail in Chapter 4.

We can also use inheritance to create our own versions of Windows Forms controls. For instance, we may want to create an enhanced TextBox control that performs some specialized validation of the input data. This can be accomplished through inheritance by creating a subclass of the original TextBox control class and enhancing it as needed. This was also covered in Chapter 4.

The same is true of Web Forms controls, where we can take an existing Web Forms control and create a subclass. Our subclass can override existing functionality or add new functionality as required. See Chapter 6 for more on this.

Summary

Of all the features requested for the new version of VB, perhaps the most common was true inheritance. As we've seen in this chapter, not only does VB.NET provide us with inheritance, but we also gain a number of other important new features and enhancements.

VB.NET dramatically enhances the way we create and work with multiple interfaces, making them far easier to use than in the past. Additionally, through the support for events as being a formal part of an interface, we can now express all the elements of an interface through this mechanism - methods, properties, and events.

For most people, the elimination of reference counting in favor of a garbage collection scheme for object termination will be a non-issue. However, it is important to be aware of this change, since an object that maintains a reference to expensive system resources will need some mechanism other than its termination to release those valuable resources.

Overall, VB.NET dramatically enhances our ability to create OO applications with VB, while preserving the vast majority of the features we have become used to in previous versions of the language.




5 RELATED COURSES AVAILABLE
MICROSOFT VISUAL BASIC V6 INTRODUCTION
To go from the fundamentals of Visual Basic programming to the threshold of Advanced level. Gaining in depth prog....
MICROSOFT VISUAL BASIC V6 ADVANCED - ACCESS BACKEND
To cover a series of advanced programming tasks and to fully command the VB programming language. Microsoft acces....
MICROSOFT VISUAL BASIC V6 ADVANCED - ORACLE BACKEND
To cover a series of advanced programming tasks and to fully command the VB programming language. Oracle is used ....
MICROSOFT VISUAL BASIC 5.0 PROFESSIONAL INTRODUCTION
To provide readers with a solid foundation upon which to build Windows applications using Visual Basic 5. Readers....
MICROSOFT VISUAL BASIC 5.0 ENTERPRISE EDITION SYSTEMS DEVELOPMENT
This course provides detailed knowledge on how to design and write applications using Visual Basic 5.0 enterprise....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Thursday 23rd May 2013  © COPYRIGHT 2013 - website design by Website Design by Visualsoft