Visual Basic 中的 Main 程序

每個 Visual Basic 應用程式都必須包含稱為 Main 的程序。 此程序是作為應用程式的起點和整體控制。 .NET Framework 會在載入您的應用程式並準備好將控制權傳遞至應用程式時,呼叫您的 Main 程序。 除非您要建立 Windows Forms 應用程式,否則您必須為自行執行的應用程式撰寫 Main 程序。

Main 包含先執行的程式碼。 在 Main 中,您可以在程式啟動時判斷要先載入哪一個表單、找出應用程式的複本是否已經在系統上執行、為您的應用程式建立一組變數,或開啟應用程式所需的資料庫。

主要程序的需求

自行執行的檔案 (通常副檔名為 .exe) 必須包含 Main 程序。 程式庫 (例如副檔名為 .dll) 本身不會執行,而且不需要 Main 程序。 您可以建立的不同類型專案需求如下:

  • 主控台應用程式會自行執行,您必須提供至少一個 Main 程序。

  • Windows Forms 應用程式會自行執行。 不過,Visual Basic 編譯器會自動在這類應用程式中產生 Main 程序,而且您不需要撰寫程序。

  • 類別庫不需要 Main 程序。 其中包括 Windows 控制項程式庫和 Web 控制項程式庫。 Web 應用程式會部署為類別庫。

宣告主要程序

有四種方式可以宣告 Main 程序。 其可能接受或不接受引數,且可能傳回或不傳回值。

注意

如果您在類別中宣告 Main,則必須使用 Shared 關鍵字。 在模組中,Main 不需要是 Shared

  • 最簡單的方式是宣告不採用引數或傳回值的 Sub 程序。

    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
    
  • Main 也可以傳回 Integer 值,作業系統會使用該值作為程序的結束代碼。 其他程式可以藉由檢查 Windows ERRORLEVEL 值來測試此程式碼。 若要傳回結束代碼,您必須將 Main 宣告為 Function 程序,而不是 Sub 程序。

    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
    
  • Main 也可以採用 String 陣列作為引數。 陣列中的每個字串都包含用來叫用程式的其中一個命令列引數。 您可以根據其值採取不同的動作。

    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
    
  • 您可以宣告 Main 來檢查命令列引數,但不傳回結束代碼,如下所示。

    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
    

另請參閱