Library tutorials & articles
New Object-Oriented Capabilities in VB.NET
Interfaces
VB
has, for some time, allowed us to create objects with more than one interface.
This was done using the Implements
keyword. Any time our class implemented a new interface we were required to
write code to implement each method on the interface. While inheritance provides
a preferable alternative to this in many cases, there are still times when we
may want to have our objects implement multiple interfaces.
VB.NET preserves the Implements keyword, and in fact the entire concept of interfaces is enhanced and made simpler as compared to VB6.
Working with Interfaces
VB.NET introduces a formalized structure for declaring interfaces. It also changes the syntax used in classes that implement an interface, making our code much more intuitive and clear.
Interface Declaration
The most visible enhancement is the introduction
of a formal syntax for declaring an interface. This is done by using the
Interface
keyword:
Public
Interface MyInterface
Event MyEvent()
Sub MyMethod()
Function MyFunction(ByVal Param1 As Integer) As Integer
Property MyProperty() As String
End
Interface
This approach is much more formal that
the approaches available in VB6. It is also more powerful – notice that not
only can we declare Sub,
Function,
and Property
methods, but also declare events as a formal part of an interface.
All the elements described in this interface must be implemented by any class that chooses to implement this interface.
Overloading Methods
Methods
(Sub
and Function)
can be declared in an interface using the Overloads
keyword. The rules for overloading are the same as we discussed earlier in the
chapter – each overloaded declaration must have a unique parameter list based
on data type of the parameters.
Declaring an interface with overloaded methods is done as in the following example:
Public
Interface MyInterface
Overloads Sub MyMethod()
Overloads Sub MyMethod(Data As String)
Overloads Function MyFunction(ByVal Param1 As Integer) As Integer
Overloads Function MyFunction(ByVal Param1 As Single) As Integer
End
Interface
When a class uses the Implements
keyword to implement an interface with overloaded methods, the class must implement
each of the overloaded method declarations.
Implementing an Interface
As with VB6,
implementing an interface is done through the use of the
Implements
keyword:
Public
Class TheClass
Implements MyInterface
End
Class
From there, however, things get a bit
different. In VB6 we’d implement the various interface elements as a set of
specially named Private
methods. That approach was unintuitive and was the cause for great confusion
among many developers new to the language. VB.NET addresses this issue by providing
a clear, concise syntax for implementing the interface – again through the application
of the Implements
keyword.
We can simply mark a method in our class as being the implementation of a specific method from the interface:
Public Sub MyMethod()
Implements MyInterface.MyMethod
So, to implement our example interface, we’d have code such as:
Public
Class TheClass
Implements MyInterface
Public Event MyEvent() Implements MyInterface.MyEvent
Public Function MyFunction(ByVal Param1 As Integer) _
As System.Integer Implements OOlib.MyInterface.MyFunction
End Function
Public Sub MyMethod() Implements OOlib.MyInterface.MyMethod
End Sub
Public Property MyProperty() As String _
Implements OOlib.MyInterface.MyProperty
Get
End Get
Set
End Set
End Property
End
Class
As with VB6, when we implement an interface, we must implement all the elements in that interface – including events, methods, and properties.
We can now create client code that interacts with our object via this interface, in addition to the object’s normal interface:
Dim
obj As MyInterface
obj
= New Implementer()
obj.MyMethod
Contrast this to VB6, where the routine to implement a method from an interface was written as follows:
Private
Sub MyInterface_MyMethod()
‘ implementation goes here
End
Sub
The fact that this method implements part
of the interface is only shown by its naming – a pretty obscure thing. We could
declare this as Public
and make it available for external use, but the name of the method couldn’t
change. Now, with VB.NET the name (and scope) of the method is independent of
the interface element being implemented.
Implementing Multiple Interfaces
A
class can have more than one Implements
statement – thus implementing more than one interface. Suppose we have the following
interfaces:
Public
Interface MyInterface
Sub DoSomething()
End
Interface
Public
Interface OtherInterface
Sub DoWork()
End
Interface
We can construct a class that implements both interfaces:
Public Class TheClass
Implements MyInterface
Implements OtherInterface
End Class
Now we have a choice. We can either implement
separate methods to handle DoSomething
and DoWork:
Private Sub DoSomething()
Implements MyInterface.DoSomething
‘ implementation
goes here
End Sub
Private Overloads Sub
DoWork() Implements OtherInterface.DoWork
‘ implementation
goes here
End Sub
Or, if they do the same thing, we can have a single method implement both methods:
Private Sub DoSomething()
_
Implements
MyInterface.DoSomething, OtherInterface.DoWork
‘ implementation goes here
End Sub
This is done by combining the list of
implemented methods in a comma-separated list following the Implements
keyword.
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.