资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vb.net数组作为参数 vba数组作为参数传入函数

详细阐述 vb.net 中main

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

创新互联建站长期为上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为新区企业提供专业的成都做网站、成都网站制作,新区网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。

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 过程。

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 ModuleMain

还可以返回一个 Integer 值,操作系统将其作为程序的退出代码。其他程序可以通过检查 Windows ERRORLEVEL 值来测试该代码。若要返回退出代码,必须将VB.NET 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 ModuleMain

还可以采用一个 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 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

可以声明VB.NET 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 argNum

End If

' Insert call to appropriate

starting place in your code.

MsgBox("The application is

terminating."

End Sub

End Module

VB.NET 里 如何将数组作为参数 传递给函数?

两个参数:一个是数组名,一个是传递的长度

Function thename(ByVal imgFrom As Image) As String

thename = imgFrom.Name

End Function

Private Sub Image1_Click()

MsgBox thename(Image1)

End Sub

一个VB.net的菜鸟问题

【1】

New Char()意思是定义一个新的Char型数组并将此数组作为参数传递

基本等同于VB6的Dim tmp() As XXXX

但是此语法要比VB6中的更加灵活

【2】

VB.net中新增了一个Char类型,用来存储Unicode字符,而用引号""括起来的字符默认为ASCII字符,所以要在引号后加上一个小写的c来强制VB将字符转换为Unicode编码。例如:

Dim UniCode As Char,ASCIICode As String

UniCode = "W"c

ASCIICode = "W"

【3】花括号为初始一个新数组时的语法,括号中可以用逗号分隔开数组的初始值。对比:

VB6:

Dim k() As String

k = Array("a","b","c")

VB.net:

Dim k As String(){"a","b","c"}

而数组作为参数传递时,在过程的一开始就定义一个传递用的数组是很麻烦的,

所以用New Char(){ "a"c , "b"c , "c"c }会很方便

【4】

Split方法有很多重载版本,你所说的这一种能够使用


分享名称:vb.net数组作为参数 vba数组作为参数传入函数
链接地址:http://cdkjz.cn/article/dohoicc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220