注册 登录
编程论坛 VB6论坛

下标越界问题请教,高手抽空帮忙看下

xiaokiss2008 发布于 2013-02-15 10:22, 292 次点击
程序代码:
Private Sub Command1_Click()
On Error GoTo Errhandler
                       '设置过滤器
  CommonDialog1.Filter = "数据文档(*.txt)|*.txt"
  CommonDialog1.Action = 1
  Text1 = CommonDialog1.FileName

 
Errhandler:
  Exit Sub
End Sub


Private Sub Command2_Click()
Dim s As String
ReDim f(m, n) As String
Dim a As String, b() As String, c() As String

Open Text1.Text For Input As #1
  m = 0
  Line Input #1, a
  b = Split(a, "    ")
  For n = 0 To UBound(b)
    f(m, n) = b(n)          这里出现下标越界
    n = n + 1
  Next n

 

 m = m + 1

 
  Line Input #1, a
  s = Replace(a, vbTab, "    ")
  c = Split(s, "    ")
  For n = 0 To UBound(c)
'    f(m, n) = b(n)      这里出现下标越界
    n = n + 1
  Next n

 
Close #1
Text2.Text = b(0)
Text3.Text = f(1, 1)
End Sub
总是提示下标越界,不懂啊 ,高手们教教
2 回复
#2
lowxiong2013-02-15 11:30
你的动态数组并没有动态调整下标,所以会出现下标越界。根据你提供的代码,你需要从文件中读取两行文本,数组m即代表行数,n是根据分隔符来决定的,由于多位数组列数必须统一,如果你的两行文本分隔符数一致的话,则程序可做如下修改:
程序代码:
Private Sub Command1_Click()
On Error GoTo Errhandler
                       '设置过滤器
  CommonDialog1.Filter = "数据文档(*.txt)|*.txt"
  CommonDialog1.Action = 1
  Text1 = CommonDialog1.FileName

Errhandler:
  Exit Sub
End Sub


Private Sub Command2_Click()
Dim s As String,f() as string
Dim a As String, b() As String, c() As String

Open Text1.Text For Input As #1
  m = 0
  Line Input #1, a
  b = Split(a, "    ")
  redim f(1,ubound(b))
  For n = 0 To UBound(b)
    f(m, n) = b(n)         
  Next n

  m = m + 1

  Line Input #1, a
  s = Replace(a, vbTab, "    ")
  c = Split(s, "    ")
  For n = 0 To UBound(c)
    if n<=ubound(b) then f(m, n) = c(n)     
  Next n

Close #1
Text2.Text = b(0)
Text3.Text = f(1, 1)
End Sub
#3
xiaokiss20082013-02-15 17:31
非常感谢回答我的提问   帮了大忙了 谢谢
1