新聞中心
詳細(xì)闡述 vb.net 中main
每個 Visual Basic 應(yīng)用程序均必須包含一個稱為VB.NET Main過程。該過程為應(yīng)用程序的起始點并為應(yīng)用程序提供總體控制。.NET Framework 在已加載應(yīng)用程序并準(zhǔn)備將控制傳遞給它時,將調(diào)用 Main 過程。除非您要創(chuàng)建 Windows 窗體應(yīng)用程序,否則就必須為自運(yùn)行的應(yīng)用程序編寫 Main 過程。
創(chuàng)新互聯(lián)建站長期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為新區(qū)企業(yè)提供專業(yè)的成都做網(wǎng)站、成都網(wǎng)站制作,新區(qū)網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
Main 中包含首先運(yùn)行的代碼。在 Main 中,可以確定在程序啟動時首先加載的窗體,確定系統(tǒng)上是否已在運(yùn)行您的應(yīng)用程序副本,為應(yīng)用程序建立一組變量,或者打開應(yīng)用程序需要的數(shù)據(jù)庫。
VB.NET Main過程的要求
獨(dú)立運(yùn)行的文件(擴(kuò)展名通常為 .exe)必須包含 Main 過程。庫(例如,擴(kuò)展名為 .dll)不獨(dú)立運(yùn)行,因而不需要 Main 過程??梢詣?chuàng)建的不同類型的項目的要求如下:
控制臺應(yīng)用程序可以獨(dú)立運(yùn)行,而且您必須提供至少一個 Main 過程。
Windows 窗體應(yīng)用程序可以獨(dú)立運(yùn)行。但是,Visual Basic 編譯器會在此類應(yīng)用程序中自動生成一個 Main 過程,因而您不需要編寫此過程。
類庫不需要 Main 過程。這些類庫包括 Windows 控件庫和 Web 控件庫。作為類庫部署 Web 應(yīng)用程序。
聲明VB.NET Main過程
有四種方法可以聲明 Main 過程。它可以使用參數(shù)或不使用參數(shù),可以返回值或不返回值。
注意
如果在類中聲明 Main 過程,則必須使用 Shared 關(guān)鍵字。在模塊中,Main 不必是 Shared。
最簡單的方法是聲明一個不使用參數(shù)或不返回值的 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 值,操作系統(tǒng)將其作為程序的退出代碼。其他程序可以通過檢查 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 數(shù)組作為參數(shù)。數(shù)組中的每個字符串均包含一個用于調(diào)用程序的命令行參數(shù)。您可以根據(jù)它們的值采取不同的操作。
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過程來檢查命令行參數(shù)而不返回退出代碼,如下所示。
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 里 如何將數(shù)組作為參數(shù) 傳遞給函數(shù)?
兩個參數(shù):一個是數(shù)組名,一個是傳遞的長度
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型數(shù)組并將此數(shù)組作為參數(shù)傳遞
基本等同于VB6的Dim tmp() As XXXX
但是此語法要比VB6中的更加靈活
【2】
VB.net中新增了一個Char類型,用來存儲Unicode字符,而用引號""括起來的字符默認(rèn)為ASCII字符,所以要在引號后加上一個小寫的c來強(qiáng)制VB將字符轉(zhuǎn)換為Unicode編碼。例如:
Dim UniCode As Char,ASCIICode As String
UniCode = "W"c
ASCIICode = "W"
【3】花括號為初始一個新數(shù)組時的語法,括號中可以用逗號分隔開數(shù)組的初始值。對比:
VB6:
Dim k() As String
k = Array("a","b","c")
VB.net:
Dim k As String(){"a","b","c"}
而數(shù)組作為參數(shù)傳遞時,在過程的一開始就定義一個傳遞用的數(shù)組是很麻煩的,
所以用New Char(){ "a"c , "b"c , "c"c }會很方便
【4】
Split方法有很多重載版本,你所說的這一種能夠使用
當(dāng)前題目:vb.net數(shù)組作為參數(shù) vba數(shù)組作為參數(shù)傳入函數(shù)
網(wǎng)站地址:http://www.dlmjj.cn/article/dohoicc.html