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
' 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:
- The Main procedure
- Input and output
- Compilation and execution
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:
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
Mainprocedure in a class, you must use the Shared keyword. In a module,
Maindoes 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:
Maincan 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
Mainas 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
Maincan 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
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
Mainto 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.
Main procedure calls the MsgBox function to display a message box containing the string
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
- Create the source file using any text editor and save it with a file name such as
- To invoke the compiler, enter the following command:
- If your program does not contain any compilation errors, the compiler creates a
- To run the program, enter the following command:
You can optionally include the /main command-line compiler option in the
vbc command to specify the namespace and module supplying
To compile and run the program from the IDE
- Create a Visual Basic console application project.
- Copy the code into the project.
- 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.