다음을 통해 공유


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 컨트롤 라이브러리 및 웹 컨트롤 라이브러리가 포함됩니다. 웹 애플리케이션은 클래스 라이브러리로 배포됩니다.

주 프로시저 선언

프로시저를 선언하는 네 가지 방법이 있습니다 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
    

참고하십시오