Export (0) Print
Expand All
5 out of 9 rated this helpful - Rate this topic

The Visual Basic Version of Hello World!

The following console program is the Visual Basic version of the traditional "Hello World!" program, which displays the string Hello World!.

' A "Hello World!" program in Visual Basic.
Module Hello
   Sub Main()
      MsgBox("Hello World!")   ' Display message on computer screen.
   End Sub
End Module

The important points of this program are the following:

  • Comments
  • The Main procedure
  • Input and output
  • Compilation and execution

Comments

The first line of the example contains a comment:

' A "Hello World!" program in Visual Basic.

The single quote (') means that the rest of the line is a comment, and will be ignored by the compiler. You can make an entire line a comment, or you can append a comment to the end of another statement, as follows:

      MsgBox("Hello World!")   ' Display message on computer screen.

The Main Procedure

Every Visual Basic program must contain a procedure called Main. This procedure serves as the starting point and overall control for your application. It is called when your module is loaded.

Note   If you declare the Main procedure in a class, you must use the Shared keyword. In a module, Main does not need to be Shared.

There are four ways to declare the Main procedure, as follows:

  • The simplest way is to declare a Sub procedure that does not take arguments or return a value, as in the earlier console program example:
       Sub Main()
    
  • Main can also return an Integer value, which the operating system uses as the exit code for your program. Other programs can test this code by examining the Windows ERRORLEVEL value. To return an exit code, you must declare Main as a Function procedure instead of a Sub procedure, as follows:
    Function Main() As Integer
       MsgBox("Hello World!")   ' Display message on computer screen.
       Return 0   ' Zero usually means successful completion.
    End Function
    
  • Main can also take a String array as an argument. Each string in the array contains one of the command-line arguments used to invoke your program. You can take different actions depending on their values. To obtain the command-line arguments, you declare Main as follows:
    Function Main(ByVal CmdArgs() As String) As Integer
       Dim ArgNum As Integer   ' Index of individual command-line argument.
       If CmdArgs.Length > 0 Then   ' See if there are any arguments.
          For ArgNum = 0 To UBound(CmdArgs)
             ' Examine CmdArgs(ArgNum) for settings you need to handle.
          Next ArgNum
       End If
       MsgBox("Hello World!")   ' Display message on computer screen.
       Return 0   ' Zero usually means successful completion.
    End Function
    
  • You can declare Main to examine the command-line arguments but not return an exit code, as follows:
    Sub Main(ByVal CmdArgs() As String)
    

For more information on the Main procedure, see Structure of a Visual Basic Program.

Input and Output

This example uses the standard Visual Basic run-time library, which is available through the Microsoft.VisualBasic namespace. You can use all the procedures and properties of Microsoft.VisualBasic without having to import it in your program.

The Main procedure calls the MsgBox function to display a message box containing the string Hello World!:

      MsgBox("Hello World!")   ' Display message on computer screen.

For more information on Windows Forms, see Microsoft.VisualBasic Namespace.

Compilation and Execution

You can compile the "Hello World!" program using either the Visual Studio .NET integrated development environment (IDE) or the command line.

To compile and run the program from the command line

  1. Create the source file using any text editor and save it with a file name such as Hello.vb.
  2. To invoke the compiler, enter the following command:
    vbc Hello.vb
    
  3. If your program does not contain any compilation errors, the compiler creates a Hello.exe file.
  4. To run the program, enter the following command:
    Hello
    

You can optionally include the /main command-line compiler option in the vbc command to specify the namespace and module supplying Main.

To compile and run the program from the IDE

  1. Create a Visual Basic console application project.
  2. Copy the code into the project.
  3. Choose the appropriate Build command from the Build menu, or F5 to build and run (corresponding to Start in the Debug menu).

For more information on the Visual Basic compiler and its options, see Building From the Command Line.

See Also

Structure of a Visual Basic Program | Microsoft.VisualBasic Namespace | Building From the Command Line | Array.Length Property | /main

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.