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

Visual Basic .NET 2008 Express - Objects in Detail
Part 6 of an About Visual Basic Tutorial

By Dan Mabbutt, About.com

Feb 19 2008

This is a free tutorial to help beginning programmers get up to speed using Microsoft Visual Basic 2008 Express. To get the most from this tutorial, you might want to start at the beginning. The Complete Table of Contents for this course can be found on the first page of part 1: A "From the Ground Up" Tutorial.

In part 5 of the tutorial, I introduced the idea of software objects. We even updated the Hello World program to object status. But objects are so important that all of this part will be devoted to them. We'll look at the concept of an object, what an object is in VB.NET, and show you the initial version of the Signature Block program. The object we code here won't be perfect, but I'll explain the problems and we'll code a better one in a later segment.

Before we look at a software object, let's look at objects in the real world. The idea of software objects has such a natural parallel in everyday life that it's worth considering. The goal is to understand how a software object works by considering how it's like a real world object.

"Software objects" and "objects" in everyday life are remarkably similar. In fact, the biggest difference is that you don't have to write detailed and correct program statements to use objects in everyday life.

Take any object - your computer, a book, your dog, the clothes you're wearing, a TV ... anything ... and you can describe it in terms of:

  • what it is
  • what it will do

Computer scientists call this:

  • state
  • behavior

In Visual Basic .NET and other languages it's called:

  • properties
  • methods

Computer scientists might also protest that these things aren't exactly alike, but they're close enough to help you understand the basic concepts.

We'll use the last set of terms - the ones for VB.NET - because that's what we're studying here. The overall structure of objects in VB.NET is shown in the illustration below.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Real World Objects and Software Objects

Consider a TV as an object. Here are some properties a TV has:

  • screen size
  • flat panel or tube
  • NTSC, SECAM, or PAL
       (broadcast standards)

Here are some methods:

  • turn on/off
  • mute the sound
  • change the channel

What makes code OOP?

We also discussed some functions of objects in part 5:

  • Inheritance
  • Polymorphism
  • Encapsulation

The idea of inheritance is a natural for TV's. Factories manufacture TV's with very specific features by model. In VB.NET, software objects are programmed as a Class and you can think of the TV model specifications as being a like a Class. A Class tells the program how to create an instance of the software object and model spec's tell the factory how to manufacture exact copies of TV's. So each actual TV is an instance of that model.

You can design a new model TV by "inheriting" most of the specifications from an earlier one and then changing a few features such as a larger screen. Because the manufacturer needs to keep track of these things, the new model would get it's own model number even though it's mostly the same as a previous one. In the same way, you can usually "inherit" most of a software object and then change a few things to create a new object.

Polymorphism is easy to demonstrate for TV's too. Polymorphism means that the object will do different things depending on what kinds of inputs are received. When the signal being received by the TV is the wireless remote, then the channel, volume, or something like that will change. When the signal being received is from the cable, then the TV turns it into sound and pictures on the screen. Different inputs create different results. With software, if you send an object a number followed by text, it will do different things than when you send it text followed by a number. (If those inputs are defined for that object. The parallel here is that your TV won't respond to your garage door opener either.)

The TV demonstrates encapsulation very well. This is the "black box" effect. In words, it means that you don't care how the TV turns DVD's and broadcasts into shows for you to watch. You only care that it does the job reliably and you can control the process. A lot of people have no idea how a TV works and they don't care. That's encapsulation.

The benefits of OOP are hard for beginners to appreciate because they become really valuable only when systems become more complex. With very simple programs, they often seem like they're more trouble than they're worth. But it's fair to say that the amazing sophistication of software wonders like the .NET Framework are only possible because OOP was used to create it.

On the next page, just some of the major reasons why OOP is better are listed.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2008 Express - Objects in Detail

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

All rights reserved.