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:
The Main procedure
Input and output
Compilation and execution
The first line of the example contains a comment:
The single quotation mark (') 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 application 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.
There are four varieties of Main:
Sub Main(ByVal cmdArgs() As String)
Function Main() As Integer
Function Main(ByVal cmdArgs() As String) As Integer
The most common variety of this procedure is Sub Main(). Unless you are creating a Windows Forms application, you must write the Main procedure for applications that run on their own. For more information, see.
Input and Output
This example uses the standard Visual Basic run-time library, which is available through thenamespace. If you compile the program in the integrated development environment (IDE), you can use all the procedures and properties of Microsoft.VisualBasic without having to import it. If you compile from the command line, you must use the in your source code, or the command-line compiler option, to make the Microsoft.VisualBasic members available to your program.
The Main procedure calls theto display a message box containing the string "Hello, World!":
Compilation and Execution
You can compile the "Hello, World!" program using either the Visual Studio 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 Hello.vb.
To invoke the compiler, enter the following command:
If your source file does not include an Imports statement for the Microsoft.VisualBasic namespace, you can include the /imports command-line compiler option in the vbc command:
vbc Hello.vb /imports:Microsoft.VisualBasic
If your program does not contain any compilation errors, the compiler creates a Hello.exe file.
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 Main.
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 press F5 to build and run (corresponding to Start in the Debug menu).
For more information on the Visual Basic compiler and its options, see.