Reputation: 61
I am trying to parse each line of a text file but it keeps giving me an error 'System.IndexOutOfRangeException'.
Each line in the 'servers.lst' is sequenced as:
Server Name|Server Host|Server Port|Server URL|Client Type|Client Version|Client URL
Each line represent one server. But I am unable to parse it correctly. It either returns the full file, blank variable, or error.
Here is my code:
Dim reader As String = My.Computer.FileSystem.ReadAllText("servers.lst")
Dim s() As String
Dim TotalServers As String = reader.Length
Dim x As Integer = 0
Dim myArray As String() = reader.Split("|"c)
For x = 1 To TotalServers
Servers(x).ServerName = myArray(0)
Servers(x).ServerIP = myArray(1)
Servers(x).ServerPort = myArray(2)
Servers(x).ServerURL = myArray(3)
Servers(x).Client = myArray(4)
Servers(x).ClientVer = myArray(5)
Servers(x).ClientURL = myArray(6)
Form1.ListBox1.Items.Add(Servers(x).ServerName)
x += 1
Next
I am trying to return each server's name but I am having difficulties.
*Edit: Here is my structure code:
Structure ServerData
Public ServerName As String
Public ServerPort As Long
Public ServerURL As String
Public ServerIP As String
Public Client As String
Public ClientVer As String
Public ClientURL As String
End Structure
Public Servers As ServerData()
Thank You.
Upvotes: 0
Views: 4664
Reputation: 20469
If you are going to read the whole list into memory anyway, you may as well use IO.File.ReadAllLines, as that returns a string array:
Dim serverDataArray as String() = IO.File.ReadAllLines("servers.lst")
For each server As String In serverDataArray
Dim serverData as String() = server.Split("|"c)
Servers(x).ServerName = serverData(0)
'etc
Next
Upvotes: 2
Reputation: 61211
You are modifying the value of x inside of your loop. That's handle for you by the looping construct. Remove the line x += 1
You have another code issue wherein you are mixing data types in this line. The Length call is going to return an integer and while that can be implicitly converted to string, it's inefficient and likely to lead to confusion long term. As noted in Lucero's answer, taking the Length of the string isn't going to tell you how many lines there were in the original file.
' Avoid
' Dim TotalServers As String = reader.Length
' Better - data types are correct but value is wrong
' Dim TotalServers As Integer = reader.Length
' Accurate type and correct count
Dim TotalServers As Integer = (reader.Split(vbCrLf)).Length
And as I'm revising this answer to discuss the further challenges I see surrounding your iteration through reader
, I see @sicKo has provided a better and more VB.NET-esque approach.
Also, your line seems questionable. Mentally parsing it, the c
there should cause a syntax error.
Dim myArray As String() = reader.Split("|"c)
Upvotes: 2
Reputation: 1256
This line of code doesn't looks right
Dim TotalServers As String = reader.Length
First of all you should split your file based on the new line. Then only, from the newly created array based on every new line, you create another array, split it with the '|' sign.
Here's my shot. This code is not tested. Just write it on the fly but I guess you should get the idea.
Dim reader As String = My.Computer.FileSystem.ReadAllText("servers.lst")
Dim x As Integer = 0
Dim arg() As String = {vbCrLf, vbLf}
Dim newLineArr() As String
Dim myArray As String()
newLineArr = reader.Split(arg, StringSplitOptions.None)
For x = 0 To newLineArr.Length - 1
myArray = newLineArr(x).Split("|"c)
Servers(x).ServerName = myArray(0)
Servers(x).ServerIP = myArray(1)
Servers(x).ServerPort = myArray(2)
Servers(x).ServerURL = myArray(3)
Servers(x).Client = myArray(4)
Servers(x).ClientVer = myArray(5)
Servers(x).ClientURL = myArray(6)
Form1.ListBox1.Items.Add(Servers(x).ServerName)
Next
Upvotes: 4
Reputation: 60190
The Length
here is the string length (in characters), and not the number of lines as you seem to expect. Also, the Dim TotalServers As String = reader.Length
doesn't make much sense; you probably want to use a generic list of a class that has the expected server members on it.
Therefore, you should read the file line-by-line and process each line, creating a new instance of the server class so that you can assign the members as you do it now.
Upvotes: 6