1. Home
  2. Computing & Technology
  3. Visual Basic

Visual Basic .NET 2008 Express - The .NET Framework and Objects

Introducing Object Oriented Programming

By Dan Mabbutt, About.com

Jan 19 2008

Traditionally (people sometimes disagree) a language had to have three features to be considered fully object oriented.

  • Inheritance
  • Polymorphism
  • Encapsulation

VB6 was often considered to fall just short because it didn't support inheritance. But VB.NET is fully OOP.

Let's take a very brief look at each of these.

Inheritance is the ability of to define a new object based on an existing object. The new object will share some or all of the first one's methods and properties. If we wanted to create another Hello World object that could also translate into Chinese, we could instantiate the first one and then simply add Chinese to what is already there.

Polymorphism is the ability of an object to do things differently when different types of data are passed to it. The Hello World object displays a message in a window when the language string of text is passed to it. What if an audio file where someone was saying the name of the language was passed? It might be very useful to have the object create an audio file output with someone speaking "Hello World" in that language.

Encapsulation means that everything the object needs is already in it and you use the object with a clearly defined interface. This kind of an object is traditionally called a "black box". The idea is "don't tell me how you do it; just do it." Because we wrote the program, we know how HelloModule.vb works, but suppose we didn't know. Suppose HelloModule.vb was a "black box and all we knew about it was that it required a language input and displayed the message. Would it work just as well? The answer is "yes". But now, suppose we discovered an even better way to do it. If we matched exactly the same interface but changed the way it worked, should anyone care? (Well, except that the translation would be "better".) The answer is "no" -- no one would care.

Hello World as an Object

In this part, we're going to code the Hello World module as an object. In part 3, it was programmed so it was not an object to make the difference as clear as possible. And I'm going to pick up the pace one more notch by assuming that you're more familiar with VB.NET Express now and fewer things need to be explained.

Rather than starting over, let's copy the solution we developed in part 3 into a new folder and change it into an object. One way to do this is to simply start with the downloaded zip file for part 3 and unzip it into a new folder. If you need the part 3 solution again, you can download it by clicking here: HelloWorld Part 3 Solution. Open the solution (HelloWorld.sln).

If you follow these instructions, you may end up with a program that isn't exactly the same as the one that you can download later. That's OK. It's more important that you do the code yourself than it is to match mine exactly. Don't be confused by all of the files and folders that are created automatically by VB.NET Express. You're not expected to know what they all are. (Most programmers probably don't know what all of them are.)

Our solution so far consists of a module HelloModule.vb and a form HelloWorld.vb (and a lot of other files and folders that VB.NET Express creates).

HelloModule.vb isn't very "OOP" because it doesn't have any properties or methods; it doesn't encapsulate anything; it can't be inherited and so forth. The way a module is being used here is just as another place to put some code. If you simply included the same code in the form, it would work the same way.

The Class - The Code Structure for Objects

To change this, we're going to add a Visual Basic .NET class. In Visual Basic .NET - and most other OOP languages - the class is the programming structure used for creating objects. As part of the change into an object, we'll update the program so that this class provides the translated message to the form as a property. The current selected language will also be a property in the class.

While we're starting out, I also changed the name of the solution to objHelloWorld just to make sure it was never confused with the part 3 solution.

The code is on the next page!

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2008 Express - The .NET Framework and Objects

©2008 About.com, a part of The New York Times Company.

All rights reserved.