1. Computing & Technology

Discuss in my forum

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

Coding Hello World As An Object

By , About.com Guide

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. Enter ...


Public Property HelloLanguage() As String = "English"

... after the Class statement and press Enter.

The HelloLanguage property will be saved in the Class as a private variable (often called a "backing store") but VB.NET 2010 doesn't require you to know these details anymore so it's hidden. The example above shows how it had to be done in the previous version compared to VB.NET 2010. The name HelloLanguage is what the property will be known as to any "outside" code. The default value of the property is "English". Before VB.NET 2010, this had to be coded in a New subroutine.

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 and press Enter.


Public ReadOnly Property HelloMessage() As String

When you press Enter this time, VB.NET's Intellisense first displays and error and then offers to add some "getter" code for you. Click the message to apply the fix, but you now have to customize this block with your own code. We will use the value of the HelloLanguage property to set the value of HelloMessage property when another object asks for it. The illustration below shows what happens.

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

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

Notice that in old version of our program, we actually had to reference the Label component in the form module to display the message:


Case "Español"
    HelloForm.lblHelloWorld.Text = "Hola Mundo"

This is bad code because a change in one module (changing the name of the Label lblHelloWorld control in HelloForm, for example) can cause the program to crash in a different module. This violates the OOP principal of "encapsulation" and is called a dependency. It's exactly the kind of problem that OOP prevents.

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


Case "Español"
    HelloMessage = "Hola Mundo"

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 HelloForm
 
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 copy of the object used in the HelloForm Class 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 of the myHello object and the SelectedIndexChanged event updates the HelloLanguage property in the object. You can double click both the Button and the ListBox to open their default event subroutines. Enter this statement into the Button Click event:


lblHelloWorld.Text = myHello.HelloMessage

And enter this statement into the SelectedIndexChanged event for the ListBox:


myHello.HelloLanguage = HelloLanguages.SelectedItem

The old Module has been replaced by our new Class so you can delete it. (Right click HelloModule.vb in Solution Explorer and select Delete.) You're done! Download the completed new code.

Our example class doesn't 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.

©2012 About.com. All rights reserved.

A part of The New York Times Company.