How do I load items from a txt file into listbox?

Thank you for your response..I'm in extreme help,completely failing ...It is a csv file and I'm just trying to load the first item in listbox...this is what I have so far
I used the load procedure
then for I as int = 0 to n
line= books(I)
data= line.split(","c)
booklist(I).title= data(0)
..... and for the rest of the categories I needed
dim query= from headings in booklist
order by headings.title
select headings
for each headings in query

You can use a For Each Loop to iterate through the array.

For Each record As String In data
    ' Do some data processing...

that's what I did...I think.its posted above.still not working..im stuck...there's definitely something im missing just don't know what

In your LINQ statement, there is no need to use Select (in c# you have to). since you are only pulling records of the same type.

I've also noticed you are pulling from one heading. since you are declare a heading as the first index of data.

data= line.split(","c)
booklist(I).title= data(0)

The foreach statement is confusing. You are using headings in the Linq statement and in the foreach statement. I would use something more like this for the Linq statement.

Dim query = From heading In data
            Order By heading.title

Then the for each loop should work.

For Each h as heading in query
    ' I'm doing data processing...

there four other categories of data (two are integers)on the same line each separated by a comma...
I guess that's why I thought I had to use the select statement...im basically teaching myself this from the web,youtube, and forums like this...and I still have no clue..everytime I debug...it says indexoutofrange

Meaning that the index you are tryng to reach does not exist.

For example your For loop:

for I as int = 0 to n

means that 'I' will increment to the next value each time the condition is ran. If I use I as a variable in the loop for example:

For I as integer = 0 to 10
    Console.WriteLine (MyArray(I)) ' write the record at the index of 'I' to console.

Now, if I have only 10 records in the array, the last index would be 9 (arrays are 0 based meaning they start at 0 and not 1). The loop will error out since the index would be higher then the number of indices in the array.

The error will be an 'IndexOutOfRange' exception; or the index I want does not exist in the array I am iterating through.

Edited 3 Years Ago by Maligui

I left out the part before the for loop.. dim n as books.count-1(thinking that would take me to the end of the file) which would be the counter...

I think this is something like you were working on.

Dim line As String
Dim data() As String

'declare the number of books variable, then assign it.
Dim n As Integer = books.count - 1

' For Loop
' This loop will run until 'I = n' or while 'I = (books.count -1)'
For I as Integer = 0 to n
    line = books(I)
    data = line.split(","c) ' divide the string by a comma, 
                            ' and assign them to the string array 'data'

' now we will iterate through the data array and pull the records.
For Each s As String in data

its weird because thats what the code looks like...with the exception of line 16..when I tried that it didnt work...I must be missing something somewhere

This article has been dead for over six months. Start a new discussion instead.