HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
VB.NET PROGRAMMING PART 1 - NEW OBJECT-ORIENTED CAPABILITIES OF VB.NET

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

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.


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


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.

VB.NET OO Implementation

VB.NET not only provides us with new OO features, but it also changes the way we implement some of the features we are used to from VB6. As we go through these features we'll cover both the new capabilities and also explore the changes to existing features.

Creating Classes

When building classes in previous versions of VB, each class got its own file. While simple, this solution could cause a larger OO project to have many files. VB.NET allows us to put more than one class in a single source file. While we don't have to take this approach, it can be nice since we can reduce the overall number of files in a project - possibly making it more maintainable.

Additionally, VB.NET provides support for the concept of .NET namespaces, as we discussed in Chapters 2 and 3. There are also changes to the syntax used to create Property methods, and we can overload methods in our classes. We'll look at all these features shortly. First though, let's look at how we add a class to a project.

Adding a class in VB.NET is similar to adding a class in VB6. In order to do this we need to create a new Windows Application project and choose the Project | Add Class menu option to bring up the Add New Item dialog:

This is the common dialog used for adding any type of item to our project - in this case it defaults to adding a class module. Regardless of which type of VB source file we choose (form, class, module, etc.) we'll end up with a file ending in a .vb extension.

It is the content of the file that determines its type, not the file extension. The IDE creates different starting code within the file based on the type we choose.

We can name the class TheClass in this dialog and, when we click Open, a new file will be added to our project, containing very simple code:

Public Class TheClass

End Class

Though a .vb file can contain multiple classes, modules and other code, the normal behavior from the IDE is the same as we've had in VB since its inception - one class, module, or form per file. We can manually add other code to the files created by the IDE with no problems, but when we ask the IDE to create a class for us it will always do so by adding a new file to the project.

At this point we're ready to start adding code.

Class Keyword

As shown in this example, we now have a Class keyword along with the corresponding End Class. This new keyword is needed in order for a single source file to contain more than one class. Any time we want to create a class in VB.NET, we simply put all the code for the class within the Class…End Class block. For instance:

Public Class TheClass
Public Sub DoSomething()
 MsgBox("Hello world", MsgBoxStyle.Information, "TheClass")
End Sub
End Class

Within a given source file (any .vb file) we can have many of these Class...End Class blocks, one after another.

Continued...


NEXT PAGE



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 8th January 2009  © COPYRIGHT 2009 - VISUALSOFT