VB.NET Main过程四种声明方法总结
创始人
2024-06-21 10:10:14
0

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

每个 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过程的相关操作方法就为大家介绍到这里。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...