Re: How to dynamically resize an array?
- From: "Family Tree Mike" <FamilyTreeMike@xxxxxxxxxxxxxxxx>
- Date: Sun, 10 May 2009 11:36:11 -0400
"Mike" <unknown@xxxxxxxxxx> wrote in message news:OG4tsIY0JHA.3988@xxxxxxxxxxxxxxxxxxxxxxx
Redim is inefficient at this redundant usage. Fragments your memory.
I would read the entire string into memory and split ;-)
Function LoadStringList(ByVal fn As String) As String()
Dim fv As StreamReader = File.OpenText(fn)
Try
' deal with MAC, OS and *nix EOL issues
Return fv.ReadToEnd().Replace(vbCrLf, Chr(10)).Split(Chr(10))
Catch ex As Exception
Console.Writeline("File I/O error: {0}",ex.message)
Finally
fv.Close()
End Try
Return Nothing
End Function
Usage:
Dim slist As String() = LoadStringList("c:\hardware-info.txt")
Console.WriteLine("Total Lines: {0}", slist.Length)
For i As Integer = 0 To slist.Length - 1
Console.WriteLine("{0,-3}:{1}", i, slist(i))
If ((i + 1) Mod 24) = 0 Then
Console.Write("Press ENTER to continue, ESC to exit => ")
If Console.ReadKey(True).Key = 27 Then Exit For
Console.WriteLine("")
End If
Next
--
roidy wrote:How do I dynamically resize an array? Take for example a program that reads and stores a series of strings from a file, but you don`t know how many strings there will be.
Not real code!!
dim test() as string
dim loop as integer = 0
while !endofFile
{
test(loop)=readStringFromFile
loop=loop+1
redim test(loop)
}
However redim destorys the perivous data. So how do I grow the size of the array at runtime?
Thanks
Rob
Have a look at File.ReadAllLines(filename).
--
Mike
.
- Follow-Ups:
- Re: How to dynamically resize an array?
- From: Mike
- Re: How to dynamically resize an array?
- References:
- How to dynamically resize an array?
- From: roidy
- Re: How to dynamically resize an array?
- From: Mike
- How to dynamically resize an array?
- Prev by Date: unable to change text in richtextbox
- Next by Date: Re: Path to my data file for Jet.OLEDB
- Previous by thread: Re: How to dynamically resize an array?
- Next by thread: Re: How to dynamically resize an array?
- Index(es):
Relevant Pages
|