Question Making an array through a file

instantwin

New member
Joined
Nov 2, 2009
Messages
4
Programming Experience
Beginner
is there a way to make an array but instead of listing the things yourself, you open a text file with a bunch of words and use those?
 
You can use either an empty array [MyArray()] you re-dimension or a List (Of String) [MyList]

VB.NET:
    Private Function LoadArray() As Integer
        Dim fileReader As System.IO.StreamReader
        Dim fileString As String
        Dim iCount As Integer = 0
        fileReader = _
        My.Computer.FileSystem.OpenTextFileReader("C:\TextFile.txt")
        While Not fileReader.EndOfStream
            fileString = fileReader.ReadLine()
            MyList.Add(fileString)
            ReDim Preserve MyArray(iCount)
            MyArray(iCount) = fileString
            iCount += 1
        End While
        Return iCount ' Return the number of elements in the array
    End Function

-Bill
 
Assuming that the file contains only the words and one per line:
VB.NET:
Dim words As String() = IO.File.ReadAllLines("file path here")

This is why I don't program for a living...I'd starve. ;) I love the elegant simplicity!
 
Back
Top