Main Procedure - Visual Basic (original) (raw)

Every Visual Basic application must contain a procedure called Main. This procedure serves as the starting point and overall control for your application. The .NET Framework calls your Main procedure when it has loaded your application and is ready to pass control to it. Unless you are creating a Windows Forms application, you must write the Main procedure for applications that run on their own.

Main contains the code that runs first. In Main, you can determine which form is to be loaded first when the program starts, find out if a copy of your application is already running on the system, establish a set of variables for your application, or open a database that the application requires.

Requirements for the Main Procedure

A file that runs on its own (usually with extension .exe) must contain a Main procedure. A library (for example with extension .dll) does not run on its own and does not require a Main procedure. The requirements for the different types of projects you can create are as follows:

Declaring the Main Procedure

There are four ways to declare the Main procedure. It can take arguments or not, and it can return a value or not.

Note

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

Module mainModule  
    Sub Main()  
        MsgBox("The Main procedure is starting the application.")  
        ' Insert call to appropriate starting place in your code.  
        MsgBox("The application is terminating.")  
    End Sub  
End Module  
Module mainModule  
    Function Main() As Integer  
        MsgBox("The Main procedure is starting the application.")  
        Dim returnValue As Integer = 0  
        ' Insert call to appropriate starting place in your code.  
        ' On return, assign appropriate value to returnValue.  
        ' 0 usually means successful completion.  
        MsgBox("The application is terminating with error level " &  
             CStr(returnValue) & ".")  
        Return returnValue  
    End Function  
End Module  
Module mainModule  
    Function Main(ByVal cmdArgs() As String) As Integer  
        MsgBox("The Main procedure is starting the application.")  
        Dim returnValue As Integer = 0  
        ' See if there are any arguments.  
        If cmdArgs.Length > 0 Then  
            For argNum As Integer = 0 To UBound(cmdArgs, 1)  
                ' Insert code to examine cmdArgs(argNum) and take  
                ' appropriate action based on its value.  
            Next  
        End If  
        ' Insert call to appropriate starting place in your code.  
        ' On return, assign appropriate value to returnValue.  
        ' 0 usually means successful completion.  
        MsgBox("The application is terminating with error level " &  
             CStr(returnValue) & ".")  
        Return returnValue  
    End Function  
End Module  
Module mainModule  
    Sub Main(ByVal cmdArgs() As String)  
        MsgBox("The Main procedure is starting the application.")  
        Dim returnValue As Integer = 0  
        ' See if there are any arguments.  
        If cmdArgs.Length > 0 Then  
            For argNum As Integer = 0 To UBound(cmdArgs, 1)  
                ' Insert code to examine cmdArgs(argNum) and take  
                ' appropriate action based on its value.  
            Next  
        End If  
        ' Insert call to appropriate starting place in your code.  
        MsgBox("The application is terminating.")  
    End Sub  
End Module  

See also