Udostępnij za pośrednictwem


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 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:

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

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()

  • 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 Main Procedure in Visual Basic.

Input and Output

This example uses the standard Visual Basic run-time library, which is available through the Microsoft.VisualBasic namespace. 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 Imports Statement (.NET Namespace and Type) in your source code, or the /imports (Visual Basic) command-line compiler option, to make the Microsoft.VisualBasic members available to your program.

The Main procedure calls the MsgBox Function (Visual Basic) to display a message box containing the string "Hello, World!":

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

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

  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

    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

  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 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 Building from the Command Line (Visual Basic).

See Also

Concepts

Main Procedure in Visual Basic

Reference

Structure of a Visual Basic Program

Imports Statement (.NET Namespace and Type)

/imports (Visual Basic)

MsgBox Function (Visual Basic)

Microsoft.VisualBasic

/main

Other Resources

Building from the Command Line (Visual Basic)