AndrewMantis
AndrewMantis

Reputation: 1

Combobox values from a textfile

Hope people can help!

I'm attempting to create a combobox filled with information from a text file thats been delimited using , as a break mark. IE

objecta,grad,N,words,5,moretext

I need the various areas between the , as integers/booleans/strings .. but I'm having problems getting the code to do this properly working

Ive searched the web, and found this code, but its not working properly for me. Maybe Im being dumb but hopefully someone can help me. Help me oh gurus of the internet!

Public Sub meritlist()
    Dim looksups As New ArrayList
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    Dim rdr As IO.StreamReader = IO.File.OpenText(c:merits.txt")
    While Not rdr.EndOfStream
        Dim line As String = rdr.ReadLine()

        'Split the line by a comma:
        Dim arrayList = line.Split(",")

        'Get the right values: break down is as: Merit Name,Type,Shared,Carthan,Max,Pre-Req
        Dim meritname As Integer = arrayList(0)
        Dim merittype As String = arrayList(1)
        Dim meritshared As Boolean = arrayList(2)
        Dim carthan As String = arrayList(3)
        Dim meritmax As Integer = arrayList(4)
        Dim prereq As String = arrayList(5)

        cmoMRDro.Items.Add(New lookups(arrayList(0), arrayList(1)))

    End While
    rdr.Close()
End Sub

Upvotes: 0

Views: 1344

Answers (1)

Pradeep Kumar
Pradeep Kumar

Reputation: 6969

There are 4 things I would suggest.

  1. Use TextFieldParser class to read delimited/fixed width text files. They are more specialized in doing this job and already have the code to handle it properly.

  2. The call to InitializeComponent is not required.

  3. Create a structure/class to store your line items, instead of raw variables. This way you have it in a more organized way.

  4. Use List of that structure/class, instead of ArrayList

While what you are already doing can be corrected with a few modifications, I would have done it this way, keeping all the suggestions I gave above. So you can see how easy it becomes after this:

Structure MeritListType
    Dim meritname As Integer
    Dim merittype As String
    Dim meritshared As Boolean
    Dim carthan As String
    Dim meritmax As Integer
    Dim prereq As String

    Public Sub New(ByVal data() As String)
        meritname = Integer.Parse(data(0))
        merittype = data(1)
        meritshared = Boolean.Parse(data(2))
        carthan = data(3)
        meritmax = Integer.Parse(data(4))
        prereq = data(5)
    End Sub
End Structure

Public Sub MeritList()
    Dim FileData As New List(Of MeritListType)
    Dim tfp As New FileIO.TextFieldParser("c:\merits.txt")
    tfp.TextFieldType = FileIO.FieldType.Delimited
    tfp.SetDelimiters(",")
    While Not tfp.EndOfData
        FileData.Add(New MeritListType(tfp.ReadFields))
    End While
    tfp.Close()

    '' you have the records in FileData
    '' do whaever you want to do with it here now

End Sub

Upvotes: 1

Related Questions