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?
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.