Varo Studio
Varo Studio

Reputation: 3

Index was outside the bounds of the array [VB.NET]

Hi i am new to VB and in the process of learning. This error occur sometimes and doesn't occur sometimes which i find it weird. I receive the error Index was outside the bounds of the array, that points to Button30.Text = Split(newestversion, vbCrLf)(**1**)

My motive is to read line by line from a online hosted text file. For example,

label1.text = line 1 of the text file
label2.text = line 2 of the text file

This is very much what i want.

Here is my current code (EDITED):

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

Thank You!!

Upvotes: 0

Views: 5506

Answers (1)

WeSt
WeSt

Reputation: 2684

You split your String for line breaks. This gives you an array, having one entry for each line in the String. However, you do not check if this array holds the amount of items you expect. You could do:

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)
...

Edit for the updated question

If you do have a problem like this, just approach it systematically and get as much information as you can. First you have to check if you really get the data you want from the remote source. To do that, add some logging (e.g. a MsgBox(newestversion) or a real log file). Check if the data you get is what you expect. If not, there's already a problem with your request/response code, which is a completely different problem than what I provided a solution for. If newestversion is OK, check if the splitting works by printing out the part() array. Maybe the server uses a different operating system or just uses vbCr as newline and not vbCrlf. If the splitting also works, you are done.

Upvotes: 1

Related Questions