|
Dieser Artikel wurde maschinell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. Weitere Informationen
|
Übersetzung
Original
|
Main-Prozedur in Visual Basic
-
Konsolenanwendungen werden eigenständig ausgeführt, und Sie müssen mindestens eine Main-Prozedur angeben. . -
Windows Forms-Anwendungen werden eigenständig ausgeführt. Jedoch generiert der Visual Basic-Compiler automatisch eine Main-Prozedur in einer solchen Anwendung, und Sie müssen sie nicht schreiben. -
Klassenbibliotheken erfordern keine Main-Prozedur. Zu diesen zählen Windows-Steuerelementbibliotheken und Websteuerelementbibliotheken. Webanwendungen werden als Klassenbibliotheken bereitgestellt.
Hinweis
|
|---|
|
|
-
Das einfachste Verfahren ist das Deklarieren einer Sub-Prozedur, die keine Argumente akzeptiert und keinen Wert zurückgibt. 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 kann auch einen Integer-Wert zurückgeben, der vom Betriebssystem als Exitcode für das Programm verwendet wird. Andere Programme können diesen Code testen, indem Sie den Windows-Wert ERRORLEVEL prüfen. Um Exitcode zurückzugeben, müssen Sie Main als eine Function-Prozedur und nicht als Sub-Prozedur deklarieren. 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 kann auch ein String-Array als Argument enthalten. Jede Zeichenfolge im Array enthält eines der Befehlszeilenargumente, die zum Aufrufen des Programms verwendet werden. In Abhängigkeit von den Werten können Sie unterschiedliche Aktionen vornehmen. 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 argNum 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 -
Sie können Main deklarieren, um Befehlszeilenargumente zu überprüfen, ohne Exitcode zurückzugeben (siehe folgendes Beispiel). 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 argNum End If ' Insert call to appropriate starting place in your code. MsgBox("The application is terminating.") End Sub End Module
Hinweis