Results 1 to 2 of 2

Thread: Populating variables from a text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    Populating variables from a text file

    In the load event of a form I have the following:

    [code Dim Prods() As String = IO.File.ReadAllLines("E:\Rajja\TxtFiles\Prod1.txt")
    Dim AAH() As String = IO.File.ReadAllLines("E:\Rajja\TxtFiles\AAH.txt")]


    The Products in the first line have previously been used to populate a listbox from which the user makes a selection. This sets a variable (n) according to the product's index in the list.

    In the current form Textbox1 displays the selected Product name.
    TextBox2 should display the Product price taken from the second textfile. In other words if n has been set to 130 then Prods(130) will display in Textbox1 and AAH(130) should display in Textbox2

    The problem is that the second textfile (AAH.txt) contains blanks, so AAH(130) is not aligned with Prods(130).

    Is there a way to read a textfile containing blanks so that the index remains consecutive?

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

    Re: Populating variables from a text file

    Firstly, you post code like this:

    [code]your code here[/code]

    not like this:

    [code your code here]

    As for the issue, are you saying that those blank lines are actually meaningless and shouldn't be there? If so then just do this:
    Code:
    Dim AAH() As String = IO.File.ReadLines("E:\Rajja\TxtFiles\AAH.txt").Where(Function(line) line <> String.Empty).ToArray()
    Note the use of ReadLines rather than ReadAllLines. Both will work but, in this case, ReadLines is more efficient.
    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

Tags for this Thread

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