Results 1 to 5 of 5

Thread: StreamReader to List

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Resolved StreamReader to List

    OK, here's my problem. I have a text file which is tab delimited. So it looks like this:

    VB Code:
    1. 21  entryA
    2. 22  entryB
    3. 23  entryC

    Anyway, it has a lot of things in it. Instead of putting all items into a listbox collection and then making a casewhere structure in the list's SelectedIndexChanged event to compare the right number to each list entry (To store in a variable), I was thinking that I would use StreamReader to read from the text file and somehow store the numbers seperately from the entries. Using casewhere means that it would be an enourmous ammount of coding (Over 200 entires).

    This allows me to:
    a) Read the file.
    b) Put the entries into the list without their corresponding numbers.
    c) When I then click on an entry, it compares it to it's corresponding number.
    d) That number is stored in a variable and the user never sees it.

    The point is to avoid using a long casewhere structure with over 200 cases. Another advantage is that what if I want to change entires in the list? So much easier. Also, another snag is that the entries in the list ARE NOT arranged according to their number, but arranged alphabetically. So this explains why I can't just get the index number of the entry from the list. I'm also not really experienced in the area of using arrays or records, but I have I feeling I'll be using them to solve this. I do know how to use streamreader though, and 1D arrays.

    I have a feeling I haven't explained something well enough or just enough. Thanks in advance.
    Last edited by martw; Nov 16th, 2005 at 01:30 AM.
    I never know what to put in this section...



    So sue me... ... ... I'm just kidding...


    www.fat-pie.com Flash Movies... You gotta see 'em to believe 'em!

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: StreamReader to List

    All you need is Strings.Split on the line read in by using streamreader.Readline... The strings.split splits on a desired string or character, in this case, a tab character. This would then return an array of two strings... so the 0 index would be the first column values and the 1 index would be your second column values... You can then do what you want with them.. add them to whichever collections you want...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: StreamReader to List

    I'm afraid I'm not quite sure what you mean, but I will say that generally the best way to read a delimited file of tabular data is with ADO.NET. You may not be experienced but you have to learn some time. I'd suggest using OleDb. Check www.connectionstrings.com for the appropriate connection string. The trick is that you treat the folder like the database and the files as the tables, so you use file names in place of table name sin your SQL statements.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: StreamReader to List

    Thanks for the help, but I don't think that solves my problem gigemboy. I've already used String.Split quite a bit.

    As for ADO.NET, I'll look into it, but I do feel that it might be well beyond my scope. If I need anymore help, or if ADO.NET doesn't solve it for me, I'll know where go

    Thanks guys
    I never know what to put in this section...



    So sue me... ... ... I'm just kidding...


    www.fat-pie.com Flash Movies... You gotta see 'em to believe 'em!

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: StreamReader to List

    well strings.split would give you the two values seperated (you code it to loop for each line in the file).. and you can then just add the lines to a datatable, and add the resulting table to a datagrid, and hide the column that shows the numerical value... (thats what I thought you were trying to do from your explanation..)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width