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

Visual Basic .NET 2008 Express - About Programming
Hello World as a Subroutine

By Dan Mabbutt, About.com

May 29 2009

It's time to write more programming code! As promised earlier, the instructions for this example program are at a little higher level this time. I won't explain every detail to keep the instructions a little shorter.

The example this time will be a Hello World program like Part 1, but this time, it will be coded as a subroutine (something we will see a lot of in future lessons) in a separate module. This is the first step in creating real objects. One of the main ideas in system development methodology is to "divide and conquer" - that is, divide programs into objects and conquer complexity. The more usual name is OOP - Object Oriented Programming. The code we will create in this lesson still falls short of being an actual object. We'll discuss why in later lessons.

We will also use quite a few new VB.NET language elements. There will be more about them in later lessons. The idea in this program is try out software development methodology, become more familiar with coding a VB.NET Express program, and get a peek at things to come. But first, let's make sure we keep our code in sync with the SDLC, starting with the System Request!

System Request
"Create a program that can display Hello World in English, Spanish, or German."

Notice that no technical jargon is used in the request. I didn't say "using OOP" for example. The System Request should be stated as simply as possible and still be fairly precise. In this case, you are the "System Owner" and the developer too. That's not a good combination in the real world, but this is a tutorial!

Analysis
As an example, I've created a very simple Use Case diagram for our program. Use Case is capitalized because it's a specific part of UML. UML doesn't have to be complex or confusing and I hope this diagram gives you a little insight into how it works. This particular diagram was created with Microsoft Visio 2003 which has extensive support for UML.

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

Design
Again, I've used Visio to create a very simple UML State Diagram for the program. The vertical boxes are called Swim Lanes in UML.

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

Programming
Because this is a course in programming, this is where we get serious.

Open the "Hello World" program from Part 1. We'll use this as a starting point for our new version.

Because we're object oriented programmers, we're going to create a new module for the part of the system that actually displays the message in different languages. So the first step is to add a new module to the project (right-click the project in Solution Explorer and select Add, then Module ...). Remember that it's a good idea to rename objects as soon as they're created (I called it "HelloModule").

A "Module" is just another file to hold VB code. Breaking up your system into logical components is the first step in making it OOP. The previous page showed a very simple Use Case diagram for our program. The main form, "HelloWorld.VB" and the module "HelloModule.vb" are just the two logical parts of our program. You can see them in Solution Explorer as shown below:

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

Click inside the module and type the statement:

Sub SayHello(ByVal HelloLanguage As String)

When you press the "Enter" key, something called Intellisense will add the End Sub statement automatically. This is a feature of VB.NET Express and Visual Studio that suggests coding for you to speed development and prevent errors. Later, when we're adding code for more event subroutines, it will be important to let Intellisense enter the code because it's just too easy to make a mistake if you enter it all on your own.

Now we're going to insert a Select Case block. This is just one way to add conditional logic to your VB.NET Express program. We'll see this topic again later in the course, but if you think about it, the way it works is easy to figure out. The variable HelloLanguage has a value assigned to it. If that value is any of the first three cases, then the statement following is executed. If it's not, the statement following Case Else is executed.

Select Case HelloLanguage
   Case "English"
      HelloWorld.HelloWorldLabel.Text = "Hello World"
   Case "Español"
      HelloWorld.HelloWorldLabel.Text = "Hola Mundo"
   Case "Deutsch"
      HelloWorld.HelloWorldLabel.Text = "Hallo Welt"
   Case Else
      HelloWorld.HelloWorldLabel.Text = "Error/Error/Fehler"
End Select

The completed HelloModule is shown in the illustration.

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

We're actually doing this in the reverse order of "top down structured programming". To do it "right" we should code the main parts of the program first and then code the "black boxes" (the "objects" that will work independently). But since this is a tutorial, we coded the module first. We'll code the rest of the system on the next page.

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 - About Programming

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

All rights reserved.