Exercise 2 - First Visual Basic Program

Objective

  • Familiarize with the Visual Basic development environment

  • Write your first Visual Basic program

We will create a simple program that displays the message Hello World on the screen.

  1. kolmebit Start the Microsoft Visual Basic .NET 2010, the Integrated Development Environment (IDE)
  2. Select File - New Project
  3. Select Console Application
  4. Call the project name HelloWorld
  5. Click on the OK button to create the project. The source code for a Visual Basic program is typically stored in one or more text files with a file extension of .vb In this example, it is called Module1.vb
  6. Add the highlighted lines in Program 2.1 Hello World to the generated source code.
  7. Press Ctrl+F5 to Start without Debugging to run your first program. (Press the Ctrl and F5 function keys at the same time)
  8. Press any key to return to the development environment.
  9. Save the project using File->Save All. The project will be stored in the location shown. To store in a different directory, click on the Browse button and select the directory you want.

Program 2.1 Hello World

Module Module1 Sub Main() Console.WriteLine("Hello World") End Sub End Module

Namespace

Namespaces provide a hierarchical means of organizing the elements of one or more programs. An analogy would the naming of cities. Do you know that there are two cities named Austin in the world? One is in the USA and the other in Argentina. If you want to send mail to either city, you would write either Austin, USA or Austin, Argentina to be unambiguous. Country names like USA and Argentina in Visual Basic are called namespaces. In Visual Basic, the default namespace is the filename you gave. For your first program, the namespace is HelloWorld

Class

Module Module1

A namespace is usually made up of one or more classes. A class is a definition for a specific kind of object. In subsequent exercises you will noticed that all the automatically generated codes have the same class called Module1. Thanks to the namespace difference, there is no confusion.

To make the transition from VB6 to VB .NET easier, the Module keyword is used instead of Class keyword. VB .NET converts the modules to classes automatically.

Fortunately for us, many classes are already predefined for the more common tasks that we need to do. In this program we make use of the Console class.

Console.WriteLine("Hello World")

The hello world output is produced using the WriteLine method of the Console Class in the System namespace.

Main

Sub Main() End Sub

The Main method is a member of the module Module1. The entry point of this application is the method named Main.

Important Notes

Please note the following while editing your Visual Basic code to avoid compilation errors

  • Visual Basic is not case–sensitive. If you type Console.Writeline as Console.writeline they are interpreted as the same. Visual Basic applies the appropriate case so if you typed writeline it is changed to Writeline when you hit enter.
  • The line continuation character _ allows you to split a statement over several lines
  • ' marks those lines for commenting and are ignored by the compiler
    ' This is a comment

End of Exercise 2