VB中数组的下界问题

来源:百度知道 编辑:UC知道 时间:2024/05/15 08:06:36
帮忙看下这段代码,提示下标越界,感觉没有越界啊。
Dim Attrdata As String
Dim I As Integer
Dim KeyAttrData() As String
Dim KeyAttrNum As Integer
KeyAttrNum = 1
Open App.Path & "\datas\" & Combo1.Text & "AD.txt" For Input As #1
Do Until EOF(1)
If I <> AttrNum Then
Line Input #1, Attrdata
If I = KeyAttr Then
KeyAttrData(KeyAttrNum) = Attrdata
KeyAttrNum = KeyAttrNum + 1
End If
Else
I = -1
End If
I = I + 1
Loop
Close 1
采纳2楼的办法问题解决了,但是又有新的问题了。这样keyattrdata()数组中的值是不是都等于keyattrdata(1)的值了??

Dim KeyAttrData() As String
这时KeyAttrData是个不含任何元素的空数组,要向其中加入值时需要先用Redim改变数组元素的个数
把下面这段:
If I = KeyAttr Then
KeyAttrData(KeyAttrNum) = Attrdata
KeyAttrNum = KeyAttrNum + 1
End If
改为
If I = KeyAttr Then
Redim Preserve(KeyAttrNum) As String
KeyAttrData(KeyAttrNum) = Attrdata
KeyAttrNum = KeyAttrNum + 1
End If

If I <> AttrNum Then
Line Input #1, Attrdata
If I = KeyAttr Then

看看你的这3句话吧
里面用到的变量 AttrNum Attrdata KeyAttr 是什么啊?
你最上面定义的变量名是 KeyAttrNum KeyAttrData 啊.!!!

Dim KeyAttrData() As String
并没定义数组大小,当然越界