Dim anArray(5, 3) As Integer

創(chuàng)新互聯(lián)主營固鎮(zhèn)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),固鎮(zhèn)h5微信小程序開發(fā)搭建,固鎮(zhèn)網(wǎng)站營銷推廣歡迎固鎮(zhèn)等地區(qū)企業(yè)咨詢
數(shù)組維數(shù)是從0開始計算的。
數(shù)組的 GetLength 方法返回指定維的長度。
anArray.GetLength(0) 表示取anArray第0維長度,0-5,即返回6
同理anArray.GetLength(1)取anArray第1維長度,0-3,即返回4
每行的數(shù)字個數(shù)不同,你說的這是不規(guī)則數(shù)組,解決方案如下:
Dim b As String = ""
Dim a As Integer()() = New Integer(9)() {}
a(0) = New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
a(1) = New Integer() {0, 3, 4, 5}
a(2) = New Integer() {0, 4, 5, 6, 7}
a(3) = New Integer() {0, 6, 5, 8, 9, 1}
a(4) = New Integer() {0, 2, 5, 1, 7}
a(5) = New Integer() {0, 4, 2, 6, 7, 3}
a(6) = New Integer() {0, 4, 5, 3, 2}
a(7) = New Integer() {0, 4, 1, 6, 2, 8, 5, 3}
a(8) = New Integer() {0, 4, 9, 6, 3, 5, 7}
a(9) = New Integer() {0, 1, 5, 9, 7, 6, 2, 4, 3, 8}
For i As Integer = 0 To a.GetUpperBound(0)
b = "第" i + 1 "行有" a(i).GetUpperBound(0) + 1 "個數(shù)" vbCrLf
Next
MessageBox.Show(b)
動態(tài)數(shù)組一般開始不能直接寫dim a as string。除非Variant。一般先定義類型 但是數(shù)組名后得加上括號。例如
DIM a() AS STRING
后面根據(jù)需要使用的時候,可以自己定義大小。但是必須注意一點(diǎn)。重新定義的時候,
Redim a(11) 的時候,就會將原來的數(shù)組元素清空了,沒有保存。
Redim Preserve a(11) 就會將重新定義數(shù)組大小的同時,還將原來的數(shù)據(jù)保存了。
下面的代碼是含有錯誤跳轉(zhuǎn)的,但是如果放在這一個函數(shù)里面不會影響你其他的代碼。
感覺完全不用錯誤處理做不出來。【雖然有個IsError,但是無法像 IsError(UBound(mArray, i))這樣使用它,不然我們就可以不用跳轉(zhuǎn)了。】
【Excel中的VBA與VB語法上沒有什么差別】
設(shè)計思想:
在VB中數(shù)姐最大維數(shù)是60,所以我們通過錯誤捕捉來處理這個問題,在這里我們用到UBound函數(shù)
Public Function ArrayRange(mArray As Variant) As Integer
Dim i As Integer
Dim Ret As Integer
Dim ErrF As Boolean
ErrF = False
On Error GoTo ErrHandle
'判斷代入的參數(shù)是否為數(shù)組
If Not IsArray(mArray) Then
ArrayRange = -1
Exit Function
End If
'VB中數(shù)組最大為60
For i = 1 To 60
'用UBound函數(shù)判斷某一維的上界,如果大數(shù)組的實際維數(shù)時產(chǎn)生超出范圍錯誤,
' 此時我們通過Resume Next 來捕捉錯這個錯誤
Ret = UBound(mArray, i)
If ErrF Then Exit For
Next i
'最后返回
ArrayRange = Ret
Exit Function
ErrHandle:
Ret = i - 1
ErrF = True
Resume Next
End Function
                新聞名稱:vb.net求數(shù)組維數(shù) vb variant 數(shù)組
                
                分享鏈接:http://www.yijiale78.com/article12/ddocpgc.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、用戶體驗、Google、云服務(wù)器、面包屑導(dǎo)航、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
