温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

VB.NET Main过程中的四种声明方法分别是什么

发布时间:2021-10-27 17:40:38 来源:亿速云 阅读:145 作者:柒染 栏目:编程语言

本篇文章为大家展示了VB.NET Main过程中的四种声明方法分别是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

VB.NET编程语言的应用范围非常广泛,其编写方式的特殊性极大的吸引了编程人员的眼光。在学习这一语言的时候,我们会遇到有关VB.NET Main过程的相关概念,那么如何才能正确理解这一过程的声明呢?大家可以在这里找到一些答案。


每个 Visual Basic 应用程序均必须包含一个称为VB.NET Main过程。该过程为应用程序的起始点并为应用程序提供总体控制。.NET Framework 在已加载应用程序并准备将控制传递给它时,将调用 Main 过程。除非您要创建 Windows 窗体应用程序,否则就必须为自运行的应用程序编写 Main 过程。

Main 中包含首先运行的代码。在 Main 中,可以确定在程序启动时首先加载的窗体,确定系统上是否已在运行您的应用程序副本,为应用程序建立一组变量,或者打开应用程序需要的数据库。

VB.NET Main过程的要求

独立运行的文件(扩展名通常为 .exe)必须包含 Main 过程。库(例如,扩展名为 .dll)不独立运行,因而不需要 Main 过程。可以创建的不同类型的项目的要求如下:

控制台应用程序可以独立运行,而且您必须提供至少一个 Main 过程。

Windows 窗体应用程序可以独立运行。但是,Visual Basic 编译器会在此类应用程序中自动生成一个 Main 过程,因而您不需要编写此过程。

类库不需要 Main 过程。这些类库包括 Windows 控件库和 Web 控件库。作为类库部署 Web 应用程序。

声明VB.NET Main过程

有四种方法可以声明 Main 过程。它可以使用参数或不使用参数,可以返回值或不返回值。

注意

如果在类中声明 Main 过程,则必须使用 Shared 关键字。在模块中,Main 不必是 Shared。

最简单的方法是声明一个不使用参数或不返回值的 Sub 过程。

  1. Module mainModule  

  2. Sub Main()  

  3. MsgBox("The Main procedure 
    is starting the application.")  

  4. ' Insert call to appropriate
     starting place in your code.  

  5. MsgBox("The application 
    is terminating.")  

  6. End Sub  

  7. End ModuleMain 

还可以返回一个 Integer 值,操作系统将其作为程序的退出代码。其他程序可以通过检查 Windows ERRORLEVEL 值来测试该代码。若要返回退出代码,必须将VB.NET Main过程声明为 Function 过程而不是 Sub 过程。

  1. Module mainModule  

  2. Function Main() As Integer  

  3. MsgBox("The Main procedure 
    is starting the application.")  

  4. Dim returnValue As Integer = 0 

  5. ' Insert call to appropriate 
    starting place in your code.  

  6. ' On return, assign appropriate 
    value to returnValue.  

  7. ' 0 usually means successful 
    completion.  

  8. MsgBox("The application is 
    terminating with error level " _  

  9. & CStr(returnValue) & ".")  

  10. Return returnValue  

  11. End Function  

  12. End ModuleMain 

还可以采用一个 String 数组作为参数。数组中的每个字符串均包含一个用于调用程序的命令行参数。您可以根据它们的值采取不同的操作。

  1. Module mainModule  

  2. Function Main(ByVal cmdArgs() 
    As String) As Integer  

  3. MsgBox("The Main procedure is 
    starting the application.")  

  4. Dim returnValue As Integer = 0 

  5. ' See if there are any arguments.  

  6. If cmdArgs.Length > 0 Then  

  7. For argNum As Integer = 0 To 
    UBound(cmdArgs, 1)  

  8. ' Insert code to examine cmdArgs
    (argNum) and take  

  9. ' appropriate action based on its value.  

  10. Next argNum  

  11. End If  

  12. ' Insert call to appropriate starting 
    place in your code.  

  13. ' On return, assign appropriate 
    value to returnValue.  

  14. ' 0 usually means successful completion.  

  15. MsgBox("The application is 
    terminating with error level " _  

  16. & CStr(returnValue) & ".")  

  17. Return returnValue  

  18. End Function  

  19. End Module 

可以声明VB.NET Main过程来检查命令行参数而不返回退出代码,如下所示。

  1. Module mainModule  

  2. Sub Main(ByVal cmdArgs() As String)  

  3. MsgBox("The Main procedure is 
    starting the application.")  

  4. Dim returnValue As Integer = 0 

  5. ' See if there are any arguments.  

  6. If cmdArgs.Length > 0 Then  

  7. For argNum As Integer = 0 To 
    UBound(cmdArgs, 1)  

  8. ' Insert code to examine cmdArgs
    (argNum) and take  

  9. ' appropriate action based on its value.  

  10. Next argNum  

  11. End If  

  12. ' Insert call to appropriate 
    starting place in your code.  

  13. MsgBox("The application is 
    terminating."  

  14. End Sub  

  15. End Module 

上述内容就是VB.NET Main过程中的四种声明方法分别是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI