Library tutorials & articles

New Object-Oriented Capabilities in VB.NET

Introduction

This is a sample chapter from Programming in VB.NET with the Public Beta

When Visual Basic 4.0 was released, it introduced a whole new era of programming for VB. Object-oriented (OO) programming was finally a possibility. Unfortunately, few OO features were included in the VB language at that point. Most notably lacking were inheritance capabilities, one of the key defining criteria for any OO language. VB was also missing a large number of secondary features such as method overloading and overriding, and constructors.

With VB.NET, the VB language finally completes the transition to a fully OO language. We now have full inheritance, along with all of the associated features we’d expect.

While it certainly remains possible to create applications that make no more use of objects than we did in VB3, these new capabilities are quite pervasive and so at least some basic understanding is required to become fully proficient in the use of VB.NET.

Generally speaking, a language is considered to be OO if it supports four main features:

  • Abstraction. VB has supported abstraction since VB4. Abstraction is merely the ability of a language to create “black box” code – to take a concept and create an abstract representation of that concept within a program. A Customer object, for instance, is an abstract representation of a real-world customer. A Recordset object is an abstract representation of a set of data.
  • Encapsulation. This has also been with us since version 4.0. It’s the concept of a separation between interface and implementation. The idea is that we can create an interface (Public methods in a class) and, as long as that interface remains consistent, the application can interact with our objects. This remains true even if we entirely rewrite the code within a given method – thus the interface is independent of the implementation.

    Encapsulation allows us to hide the internal implementation details of a class. For example, the algorithm we use to compute Pi might be proprietary. We can expose a simple API to the end user, but we hide all of the logic used for our algorithm by encapsulating it within our class.

  • Polymorphism. Likewise, polymorphism was introduced with VB4. Polymorphism is reflected in the ability to write one routine that can operate on objects from more than one class – treating different objects from different classes in exactly the same way. For instance, if both Customer and Vendor objects have a Name property, and we can write a routine that calls the Name property regardless of whether we’re using a Customer or Vendor object, then we have polymorphism.

    VB, in fact, supports polymorphism in two ways – through late binding (much like Smalltalk, a classic example of a true OO language) and through the implementation of multiple interfaces. This flexibility is very powerful and is preserved within VB.NET.

  • Inheritance. VB.NET is the first version of VB that supports inheritance. Inheritance is the idea that a class can gain the pre-existing interface and behaviors of an existing class. This is done by inheriting these behaviors from the existing class through a process known as subclassing. With the introduction of full inheritance, VB is now a fully OO language by any reasonable definition.

In this chapter we’ll discuss all these new features and what they mean to us as VB developers.

Merger of Object- and Component-Oriented Concepts

Since version 4.0, VB has provided us with the ability not only to create objects, but also COM components. In fact, VB’s ability to create objects, and the way VB objects worked, was very closely tied to COM and the way objects worked within and between COM components.

In COM, components are pre-compiled binary entities – typically a DLL, EXE or OCX file. Each component contains one or more classes that can be used by client applications. In VB6 we could create ActiveX EXE, ActiveX DLL or user control (OCX) projects – thus creating COM components. Even when we weren’t creating these project types, the way VB6 allowed us to create and work with objects was defined by how objects were created within COM components.

It comes as no surprise then that VB.NET is very closely tied to the way .NET handles objects and the way objects work within and between .NET assemblies or components. This represents a pretty substantial change for VB, however, since .NET is substantially more advanced in terms of objects and components as compared to COM.

With COM, objects and components were interrelated, but the marriage of the two was far from seamless. In .NET on the other hand, OO and component-oriented concepts (as defined earlier) are very closely related in ways that we could only dream of in the COM environment.

In .NET we retain the component-oriented features we are used to:

  • Component-level scoping via the Friend keyword
  • Ability to implement interfaces with the Implements keyword

Component-level scoping allows us to create classes or methods that are available to all the other code in our component – but not to code outside our component. We’ll discuss this in more detail later, but basically it is a level of scoping that sits between Private and Public – and is accessed via the Friend keyword.

Implementing interfaces via the Implements keyword allows each of our classes to have several different “identities” – each with its own interface. This is a powerful technique that is used widely within both COM and .NET, and is something we’ll discuss in more detail later.

In addition to these existing features, with .NET we also gain some strong capabilities – most importantly inheritance and the Inherits keyword.

VB.NET benefits from this new, closer relationship between objects and components – making both types of concept central to the language and to the way we develop applications.

Let’s walk through the OO features in VB.NET.

Comments

  1. 12 May 2005 at 01:06

    application level

  2. 04 Nov 2004 at 12:10

    Quote:
    [1]Posted by bsol on 19 Mar 2004 04:06 PM[/1]
    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....

  3. 19 Mar 2004 at 16:06

    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.

  4. 02 Jul 2003 at 03:14

    How do we read the attributes in an XML file using a DataSet?

  5. 24 Feb 2003 at 08:50

    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.

  6. 21 Nov 2002 at 04:46

    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?

  7. 01 Nov 2002 at 06:35

    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...

  8. 01 Jan 1999 at 00:00

    This thread is for discussions of New Object-Oriented Capabilities in VB.NET.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related jobs

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!