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.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It works on my machine.” - Anonymous