Visual Basic

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

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

Coding Hello World As An Object

By Dan Mabbutt, About.com

Jan 19 2008

Right-click the solution and select Add, then Class .... I named my class TransHello. We know that we're going to need a property to hold the language "Hello World" will be translated to, so let's add that first. To do that enter ...

Public Property HelloLanguage() As String

... after the Class statement and press Enter. Note that Intellisense enters the rest of the whole property structure for you! We'll fill this out with a little code soon.

Public Property HelloLanguage() As String
   Get

   End Get
   Set(ByVal value As String)

   End Set
End Property

The HelloLanguage property will be held in the class as a private variable, which is normally called a member variable. HelloLanguage is what the property will be known as to any "outside" code. So add a line above the Property statement for the member variable:

Private m_HelloLanguage As String

The use of the "m_" prefix for private member variables is a common convention.

To provide this property to other code, simply assign it. Add this statement inside the Get block:

HelloLanguage = m_HelloLanguage

To change the property from outside this class, add this statement inside the Set block:

m_HelloLanguage = HelloLanguage

And since we know what our property is now, change the Set block to:

Set(ByVal HelloLanguage As String)

But what if the HelloLanguage property is accessed before any value is stored in m_HelloLanguage? To make our code more bulletproof, let's give our property a default value using what is called a constructor. The class we're coding is just a pattern for the objects that will use this class. Whenever it's used, a new object will be created using this pattern. We want each of these new objects have a default value for the HelloLanguage property. The way to do this is with a subroutine named, appropriately enough, New. This subroutine is called whenever a new object is created using this class as a pattern. So enter this code to do that:

Public Sub New()
   m_HelloLanguage = "English"
End Sub

The TransHello object has two properties, however. The second one, HelloMessage depends on the value of the HelloLanguage property. Since the second property is set using the value of the first, (and is never set from outside), it's called a ReadOnly property. To code it, enter this statement just below the HelloLanguage property block and press Enter.

Public ReadOnly Property HelloMessage() As String

When you press Enter this time, Intellisense only codes the Get block for you because you used the ReadOnly keyword.

The code inside the Get block is the same Select Case code (with some minor changes) that you used in the part 3 code. So copy that code into the Get block. (It's in HelloModule.vb.)

The first change you need to make is to use our internal value m_HelloLanguage for the decision variable of the Select statement:

Select Case m_HelloLanguage

Now, notice that in our part 3 program, we actually had to reference the Label component in the form module to display the message:

HelloWorld.HelloWorldLabel.Text = "Hello World"

This is bad code because a change in one module (changing the name of the Label component, for example) can cause the program to crash in a different module. This is called a dependency and it's the kind of thing that OOP is designed to prevent. And it does!

Instead, we just change the value returned by the Get block:

Case "English"
   HelloMessage = "Hello World"

Much better! After you change the rest of the Select block in the same way, we're finished with the class.

Thanks to OOP, the main form can be improved too. In fact, almost every line of code in the form is obsolete now, so delete all of it in the code window leaving only an empty Class block

Public Class HelloWorld

End Class

Both to introduce a new component and to illustrate how our class works better, replace the entire RadioButton GroupBox with a ListBox control instead. (I named mine HelloLanguages.) You can enter the three language choices ...

English
Español
Deutsch

... into the Items collection in the properties for the ListBox.

To use our new object, we have to instantiate it first. So this is the first statement in the HelloWorld class code:

Dim myHello As New TransHello

The TransHello class is the template for our new object, but the instantiated new copy of the object is called myHello. This is an important concept to understand. You seldom work with the class directly. You normally use a copy of the class.

There are only two lines of code that need to be entered into the default events for the Button and the ListBox now. The Click event for the Button simply displays the HelloMessage property value and the SelectedIndexChanged event updates the HelloLanguage property in the object. You can doubleclick both the Button and the ListBox to open their default event subroutines. Enter this statement into the Button Click event:

HelloWorldLabel.Text = myHello.HelloMessage

And enter this statement into the SelectedIndexChanged event:

myHello.HelloLanguage = HelloLanguages.SelectedItem

Since you don't need the module anymore, you can delete it. (Right click HelloModule.vb in Solution Explorer and select Delete.) You're done! Download the completed objHelloWorld code.

Our example class does not illustrate a method yet. The only thing our class does is hold property values and supply them to the form when needed. But even that has simplified our code and made it easier to code and less likely to have some nasty bug in it. We'll get to examples that show you more of the advantages of OOP later in the course.

In part 6, Objects in Detail, a number of concepts - including OOP, analysis, and program coding - are tied together and then used to start the development a more significant program: the About Visual Basic Signature Block program.

Explore Visual Basic

By Category

About.com Special Features

Visual Basic

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

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

All rights reserved.