You are here:About>Computing & Technology>Visual Basic> Learn VB.NET> Visual Basic .NET 2008 Express - About Programming
About.comVisual Basic
Newsletters & RSSEmail to a friendSubmit to Digg

Visual Basic .NET 2008 Express - About Programming

From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Jan 19 2008

Hello World as a Subroutine

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").

Click inside the module and type the statement:

Sub SayHello(ByVal HelloLanguage As String)

Notice the Intellisense. That's a feature of VB.NET Express and Visual Studio that suggests coding for you to speed development and prevent errors. Intellisense adds the End Sub statement automatically as soon as you press the Enter key.

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

It should be noted that we're 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". But since this is a tutorial, we coded the subroutine first. Now that we have a working subroutine, we can code the rest of the system. We do that on the next page.

 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.