共用方式為


Visual Basic 中的主要程序

每個 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 值來測試此程式碼。 若要傳回結束代碼,您必須宣告 MainFunction 程式,而不是 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
    

另請參閱