【问题标题】:Index was outside the bounds of the array [VB.NET]索引超出了数组的范围 [VB.NET]
【发布时间】: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


【解决方案1】:

您将String 拆分为line breaks。这为您提供了一个数组,String 中的每一行都有一个条目。但是,您不会检查此数组是否包含您期望的项目数量。你可以这样做:

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
    MsgBox(String.Format("Array only has {0} items", part.Length))
    Exit Sub 
End If

'updates new episode numbers on buttons
Button20.Text = part(0)
Button30.Text = part(1)
Button40.Text = part(2)
...

编辑更新的问题

如果您确实遇到这样的问题,只需系统地处理它并尽可能多地获取信息。首先,您必须检查您是否真的从远程源获得了您想要的数据。为此,请添加一些日志记录(例如 MsgBox(newestversion) 或真实日志文件)。 检查您获得的数据是否符合您的预期。如果不是,那么您的请求/响应代码已经存在问题,这与我提供的解决方案完全不同。如果newestversion 正常,则通过打印出part() 数组来检查拆分是否有效。也许服务器使用不同的操作系统,或者只是使用vbCr 作为换行符而不是vbCrlf。如果拆分也有效,那么您就完成了。

【讨论】:

  • 它没有用。它没有读取和更改按钮的文本
  • 检查正在发生的事情的最简单方法是在某处设置breakpoint 并逐步完成该过程。必须发生以下情况之一:在某处引发异常OR 您看到消息框然后方法退出OR 按钮具有您希望它们具有的文本。如果您不想调试,我建议您将newestversion 的内容记录下来(记录到文件、控制台或使用消息框)。
  • 没有任何错误。您提供的上面的代码没有显示错误,也没有从文本文件中检索数据):
  • 嗯,当然,你必须把它放在你代码中的正确位置。 Dim newestversion As String = sr.ReadToEnd 行也存在于您在上面发布的代码中。只需将我给您的代码粘贴到该位置即可。再次澄清一下:您需要使用您发布的代码并将我提供的片段放入该代码中。您可能已经注意到我也使用sr.ReadToEnd。这个sr 实例是您在问题代码中声明的实例。
  • 是的,我想我确实让我用新的编辑来编辑我的帖子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 2010-11-17
相关资源
最近更新 更多