【发布时间】:2015-04-19 05:35:40
【问题描述】:
您好,我是 VB 新手,正在学习中。这个错误有时会发生,有时不会发生,我觉得很奇怪。
我收到错误Index was outside the bounds of the array,它指向Button30.Text = Split(newestversion, vbCrLf)(**1**)
我的动机是从在线托管的文本文件中逐行读取。 例如,
label1.text = line 1 of the text file
label2.text = line 2 of the text file
这正是我想要的。
这是我当前的代码(已编辑):
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("direct link to my online txt file")
Dim response As System.Net.HttpWebResponse = request.GetResponse
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream)
Dim stringReader As String
stringReader = sr.ReadLine()
Button10.Text = stringReader
Dim newestversion As String = sr.ReadToEnd
Dim currentversion As String = Application.ProductVersion
Dim part() As String = Split(newestversion, vbCrLf)
If part.Length < 10 Then
' not enough items in the array. You could also throw and exception or do some other stuff here
Label10.Text = "beta"
Exit Sub
End If
'updates new episode numbers on buttons
Button20.Text = part(0)
Button30.Text = part(1)
Button40.Text = part(2)
Button50.Text = part(3)
Button60.Text = part(4)
Button70.Text = part(5)
Button80.Text = part(6)
Button90.Text = part(7)
Button100.Text = part(8)
Button110.Text = part(9)
End If
谢谢!!
【问题讨论】:
-
如错误所示,您的
newestversion变量包含的行数不足。 -
使用 System.IO.File.ReadAllLines() 代替。注意返回数组的 Length 属性,如果太低就知道文件包含垃圾。
标签: vb.net visual-studio vb.net-2010