Luke Holland Luke Holland - 2 months ago 6
Vb.net Question

Split() doesn't work properly

well I'm doing a computing assessment and well I've ran into an issue with splitting a string. For some reason when the string splits the array stores the whole thing in Variable(0). The error that occurs is when it tries to assign TicketID(Index) a value, it says that the array is out of bound.

Here's the code:

Private Sub ReadInformation(ByRef TicketID() As String, CustomerID() As String, PurchaseMethod() As Char, NumberOfTickets() As Integer, FileName As String)

Dim Line, TextArray(3) As String
Dim Index As Integer

FileOpen(1, FileName, OpenMode.Input)

For Index = 0 To 499

Input(1, Line)
TextArray = Line.Split(",")

CustomerID(Index) = TextArray(0)
TicketID(Index) = TextArray(1)
NumberOfTickets(Index) = TextArray(2)
PurchaseMethod(Index) = TextArray(3)

MessageBox.Show(CustomerID(Index))

Next

FileClose()

End Sub


Here's the first 10 lines of the TextFile I'm trying to read:

C001,F3,10,S
C002,F3,2,O
C003,F3,3,S
C004,W2,9,S
C005,T3,10,S
C006,F3,2,S
C007,W1,3,O
C008,W3,1,O
C009,T2,2,S
C010,F2,9,O


Here's the Error Message I receive:

Error Message

Answer

I would use some Lists instead of arrays. In this way you don't have to worry about length of the arrays or if there are fewer lines than 500. Of course, using the more advanced NET Framework methods of the File.IO namespace is a must

Private Sub ReadInformation(TicketID As List(Of String), _
                            CustomerID As List(Of String), _
                            PurchaseMethod As List(Of Char), _
                            NumberOfTickets As List(Of Integer), _
                            FileName As String)

    for each line in File.ReadLines(FileName)
        Dim TextArray = Line.Split(","c)
        if TextArray.Length > 3 Then
            CustomerID.Add(TextArray(0))
            TicketID.Add(TextArray(1))
            ' This line works just because you have Option Strict Off
            ' It should be changed as soon as possible
            NumberOfTickets.Add(TextArray(2))
            PurchaseMethod.Add(TextArray(3)) 
        End If
    Next

End Sub

You can call this version of your code declaring the 4 lists

Dim TicketID = New List(Of String)()
Dim CustomerID = New List(Of String)()
Dim PurchaseMethod = New List(Of Char)()
Dim NumberOfTickets = New List(Of Integer)()
ReadInformation(TicketID, CustomerID, PurchaseMethod, NumberOfTickets, FileName)

Another approach more Object Oriented is to create a class that represent a line of your data. Inside the loop you create instances of that class and add the instance to a single List

Public Class CustomerData
     Public Property TicketID As String
     Public Property CustomerID As String
     Public Property NumberOfTickets As Integer
     Public Property PurchaseMethod As Char
End Class

Now the loop becomes

Private Function ReadInformation(FileName As String) as List(Of CustomerData)

    Dim custData = New List(Of CustomerData)()
    For Each line in File.ReadLines(FileName)
        Dim TextArray = Line.Split(","c)
        if TextArray.Length > 3 Then
            Dim data = new CustomerData()
            data.CustomerID = TextArray(0)
            data.TicketID = TextArray(1)
            data.NumberOfTickets = TextArray(2)
            data.PurchaseMethod = TextArray(3)
            custData.Add(data)
        End If
    Next
    return custData
End Function

This version requires the declaration of just one list

You can call this version of your code passing just the filename and receiving the result fo the function

Dim customers = ReadInformation(FileName)
For Each cust in customers
    Console.WriteLine(cust.CustomerID)
    ...
Next

Or use it as an array

Dim theFirstCustomer = customers[0]
Console.WriteLine(theFirstCustomer.CustomerID)
Comments