VB.NET Main过程四种声明方法总结

开发 后端
VB.NET Main过程的声明可以通过四种不同的方法来进行。编程人员在实际开发中可以看、根据自己不同的需求来进行选择。

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

责任编辑:曹凯 来源: wewill.cn
相关推荐

2009-11-02 10:36:16

VB.NET Sub

2010-01-22 10:41:33

VB.NET声明结构

2010-01-21 16:37:56

VB.NET变量声明

2011-05-20 16:34:35

VB.NET

2009-11-03 10:51:33

VB.NET共享

2010-01-19 16:55:46

VB.NET声明语句

2010-01-19 14:26:45

VB.NET递归过程

2009-11-11 13:24:29

VB.NET事件

2010-01-19 09:36:06

VB.NET Func

2009-10-12 12:54:58

VB.NET声明API

2010-01-15 15:39:14

VB.NET Sub过

2010-01-13 14:32:11

VB.NET确定文件版

2010-01-15 15:03:23

VB.NET对象变量声

2009-11-02 13:07:26

VB.NET OPEN

2009-11-10 13:08:13

VB.NET编程技巧

2010-01-20 17:54:13

VB.NET特殊字符

2009-11-02 13:14:18

VB.NET函数

2009-11-11 14:34:14

VB.NET动态连接库

2009-11-03 09:26:13

VB.NET方法

2009-10-28 09:21:19

VB.NET技术
点赞
收藏

51CTO技术栈公众号