Library tutorials & articles
New Object-Oriented Capabilities in VB.NET
- Introduction
- VB.NET OO Implementation
- Object Lifecycle
- Inheritance
- Shared or Class Members
- Events
- Interfaces
- Interacting with Objects
- Cross-Language Inheritance
- Summary
Cross-Language Inheritance
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.
Related articles
Related discussion
-
How can I execute server-side function using asp.net Ontextchanged orJavascript onchange?
by mamoru0916 (0 replies)
-
Excel Oledb Engine and VB.NET
by anand.lv (1 replies)
-
we search the company in India for program creation under the order
by anand.lv (1 replies)
-
VB.NET Windows: Getting Windows Message from external class without overriding WndProc
by CallMeLaNN (0 replies)
-
String size limit and array upperbound limit
by Akhtar Hussain (2 replies)
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum)
Events coming up
-
Dec
6
Developing AJAX Web Applications with Castle Monorail
London, United Kingdom
Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!
application level
Error in the article "New Object-Oriented Capabilities in VB.NET - Events"
Derived classes cannot raise base class events in VB.Net. Handle them sure, but not raise them - even if they are declared public. In order to achieve this handle the base class event and raise a derived class event instead.
B.
While it is true that VB.NET cannot directly raise events in base classes from a derived class, there is an easy workaround. BTW, in C# you can simply call an event in a base class like: base.onMyEventName(EventArgs e).
But in VB.NET you cannot use MyBase.EventName() at all. But the workaround is an easy one.
1) In the base class add an overridable sub that simply raises an event defined in the base class.
Public MustInherit Class MyClass
Protected Event MyEventName(ByVal e as EventArgs)
Protected Overridable Sub OnMyEventName(ByVal e as EventArgs)
Raiseevent MyEventName(e)
End Sub
End Class
2) In the derived class make a call to the overridable method: Me.OnMyEventName(New EventArgs). It's really just that easy. Do it all the time.
Have Fun....
Error in the article "New Object-Oriented Capabilities in VB.NET - Events"
Derived classes cannot raise base class events in VB.Net. Handle them sure, but not raise them - even if they are declared public. In order to achieve this handle the base class event and raise a derived class event instead.
B.
How do we read the attributes in an XML file using a DataSet?
boig,
I needed the same thing and found that I have it working using the Public Shared declaration on the class that contains my XMLDocument and the functions to access the document. This makes the class available across the application.
Public Shared oTriggers As Triggers
If you only need the XMLDocument I would give that a try.
Hope this helps.
I need a global variable; but it is a "XmlDocument" object, does anybody know how do I have to create it, and how to initialitze it?
If you set a shared property of a user class in a ASP.NET application, how long wil the value last? Application level? Session level? Page level? Well i was actually looking for an answer to that when i got here... useful article anyway even though it doesn't answer my question...
This thread is for discussions of New Object-Oriented Capabilities in VB.NET.