关于下标越界的问题,同一段代码在不同程序中一个正常一个下标越界,求助高手
Subsosuofile(MyPathAsString)DimMynameAsStringDimaAsStringDimB()AsStringDimdir_i()AsSt...
Sub sosuofile(MyPath As String)
Dim Myname As String
Dim a As String
Dim B() As String
Dim dir_i() As String
Dim i, idir As Long
If Right(MyPath, 1) <> "\" Then MyPath = MyPath + "\"
Myname = Dir(MyPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
Do While Myname <> ""
If Myname <> "." And Myname <> ".." Then
If (GetAttr(MyPath & Myname) And vbDirectory) = vbDirectory Then '如果找到的是目录
idir = idir + 1
ReDim Preserve dir_i(idir) As String
dir_i(idir - 1) = Myname 这里越界
Else
List1.AddItem MyPath & Myname '把找到的文件显示到列表框中
End If
End If
Myname = Dir '搜索下一项
Loop
For i = 0 To idir - 1
Call sosuofile(MyPath + dir_i(i))
Next i
ReDim dir_i(0) As String
End Sub 展开
Dim Myname As String
Dim a As String
Dim B() As String
Dim dir_i() As String
Dim i, idir As Long
If Right(MyPath, 1) <> "\" Then MyPath = MyPath + "\"
Myname = Dir(MyPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
Do While Myname <> ""
If Myname <> "." And Myname <> ".." Then
If (GetAttr(MyPath & Myname) And vbDirectory) = vbDirectory Then '如果找到的是目录
idir = idir + 1
ReDim Preserve dir_i(idir) As String
dir_i(idir - 1) = Myname 这里越界
Else
List1.AddItem MyPath & Myname '把找到的文件显示到列表框中
End If
End If
Myname = Dir '搜索下一项
Loop
For i = 0 To idir - 1
Call sosuofile(MyPath + dir_i(i))
Next i
ReDim dir_i(0) As String
End Sub 展开
展开全部
越界的那个程序里是不是有 Option Base 1 这句话
正常的那个没有或者有 Option Base 0
正常的那个没有或者有 Option Base 0
追问
果然是这个原因,但如果在一个程序里我声明的动态数组有的需要从下标0开始有的需要从1开始怎么办呢?
option base只能用一次
谢谢!
追答
你可以在定义的时候指定
Dim arr(0 to n) as String ‘下标0开始
Dim arr2(1 to m) as String ’下标1开始
下面这个也可以,下标从2开始
Dim arr3(2 o k) as String
在你的程序里可以在重新定义的时候做:
ReDim Preserve dir_i(1 to dir) as String
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询