VB 新手,我在讀取資料的時候總是報下表越界,請大神幫忙
找下問題,感激不盡,代碼如下:
Open "test.txt" For Input As #1
Dim x, y, b, d, g
Do While Not EOF(1)
'------------取檔案內容
Line Input #1, d
i = i + 1
If i > 2 Then
d = Replace(d, " ", ",")
g = Split(d, ",")
x=g(1)
y=g(2)
b(x,y)=g(3)
End If
Loop
Close #1
資料格式如下,
'#####################test.txt##########
RawData
2 9 11 11 0
2 10 21 21 0
3 9 21 21 0
uj5u.com熱心網友回復:
在VB IDE里面運行,出錯彈出對話框,點除錯按鈕,游標將自動定位到出錯行。此時在立即視窗中,可以使用
?變數名
命令顯示當時對應變數當時的值。
uj5u.com熱心網友回復:
1 下標越界的原因是 b 沒有實體化為能夠容納 x * y 個元素的陣列。2 資料結構的設計也不合理。b 必然成為一個稀疏的陣列,效率很低。
不妨將 b 設計為一個結構:
Private Type Point_Value
x As Long
y As Long
V As Long
End Type
Open "test.txt" For Input As #1
Dim b() As Point_Value, strLine As String, strItem() As String
Do While Not EOF(1)
'------------取檔案內容
Line Input #1, strLine
i = i + 1
If i > 2 Then
strItem = Split(strLine, " ")
Redim Preserve b(i - 3) As Point_Value
If Ubound(strItem) >= 3 Then
b(i-3).x = strItem(1)
b(i-3).y = strItem(2)
b(i-3).V = strItem(3)
End If
End If
Loop
Close #1
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/125069.html
標籤:VB基礎類
上一篇:利用VB實作網頁MP3檔案下載
