VB.NET 1.1 Tutorial - Hello Mum Console Application


Most programming tutorials start with a simple program that displays "Hello world" on the screen. Well being a family man, I decided we would say hello to mum instead.

Back in the good old days of DOS, every application ran from the command line, so our first application will be a command line version of Hello Mum. In the Listing below, I have written the whole of the program. We will run it first, and then break down each line to understand the basics of a VB program.

Hello Mum Console Application

' Allow easy reference to the System namespace classes.
Imports System

' This module houses the application's entry point.
Public Module HelloMum
  ' Main is the application's entry point.
  Public Sub Main()
    ' Write text to the console.
    Console.WriteLine( "Hello Mum using Visual Basic!" )
    Console.ReadLine()
  End Sub
End Module 

Copy and paste the text into notepad and save the file as hello.vb. The .vb file extension means that it is a visual basic file. Alternatively, you can download the file hello.vb from the link. If you have Visual Studio, you can download the solution here.

Now open a Command Line Prompt in the directory where you saved the file and type the following:

vbc /t:exe /r:system.dll hello.vb

This will build the visual basic code into an executeable program. To run the program type the following in the command line.

hello

"Hello Mum using Visual Basic!" will appear on the following line. Close the command line by typing "exit". You can download my copy of the executable from here.

When compiling I find it easier to create a batch file as this is a lot quicker than typing all of the dll names needed every time you rebuild the application. You can download my batch file here. Save it in the same directory as the file hello.vb.

Imports

The first two lines of code deal with importing a namespace. The first line is a comment.

' Allow easy reference to the System namespace classes.
Imports System 

In this example, the Imports System declaration, tells the program that it can reference the code in the System namespace without adding the word System to every place a reference would need it. If we did not use the Imports keyword, the program statement below would look like the following:

System.Console.WriteLine( "Hello Mum using Visual Basic!" )

Modules

The second two lines of code deal with the Module keyword. Again the firstline is a comment. The second line declares a standard Module called "HelloMum".

' This module houses the application's entry point.
Public Module HelloMum

The standard-module declaration finishes with the End keyword.

End Module

The purpose of standard modules is to hold code that is outside of any class definition. Within them you can define data-types that cannot be instantiated and whose members are all static.

Before we move on, there is one keyword that we have not mentioned about. The Public keyword. This is an access modifiers that means that this module is visible to any method of any class.

The Main Method (Subroutine)

The third two lines of code deal with the only method in the program. Again the first line is a comment.

' Main is the application's entry point.
Public Sub Main()

The Sub keyword statement begins the definition of a subroutine (a method that has no return value), and always finishes with the End keyword:

End Sub

The Main subroutine is where every program starts it's execution. When the Visual Basic .NET compiler is invoked, it looks for a subroutine named Main in one of the classes or standard modules exposed by the application. The Main subroutine must be declared as Public, and there must be one and only one Main subroutine in the applciation.

Program Statements

The last three lines of code deal with the actual printing of the line "Hello Mum using Visual Basic!" on the console. Again the first line is a comment. The second line uses the method WriteLine of the class Console in the namespace System to write "Hello Mum using Visual Basic!" on the console.

' Write text to the console.
Console.WriteLine( "Hello Mum using Visual Basic!" )
Console.ReadLine()

To stop the console from disappearing when the application is executed through windows, the third line is there so that the text stays on screen until a key is pressed on the keyboard.


References

For more information on the System namespace, visit MSDN at microsoft here.

For more information on the Console class, visit MSDN at microsoft here.

For more information on the WriteLine method, visit MSDN at microsoft here.

For more information on the ReadLine method, visit MSDN at microsoft here.

What Next?

Next we will create the same program, but for windows instead of the console.

Return to the Tutorial Contents.