0

So I'm trying to populate an array from a text file. I'm in Visual Basic (which I haven't touched in over a year, and have very limited knowledge from a High School course.) I have the text being read and I attempted to put it into an array from various other resources online, except that the array isn't really being read. The last value in the text file is the value that's being stored, and I'm not really sure how to fix it. Here is the code I have so far:

Dim sr As New StreamReader("text file location")       
Dim words(292) As String
Dim text as String = ""
Dim i As Integer = 0
    Do Until sr.Peek = -1
        text = sr.ReadLine()
        words(i) = text
        lstWords.Items.Add(words(i))
    Loop

I'm new to the StackOverFlow community, and would love some help from anyone who is able to give it! Thank you in advance!

1 Answer 1

2

You're doing it the hard way. Try this:

Dim words() As String = File.ReadAllLines("text file location") 

And since you're loading a listbox:

lstWords.Items.AddRange(File.ReadAllLines("text file location"))
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome, thank you so much! How would I refer to one of the words in the array? Would it just be words(i), if I were looping through something?
words(i) would work. If you use the 2nd option, lstWords.Items(i)
You're a life saver, thank you so much. I have to loop through each of the words in the listbox individually, would I just have to set it up through a timer? I have it setup right now, except in the loop it goes from the beginning to end quick. Any idea why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.