Re: How to dynamically resize an array?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"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

.



Relevant Pages

  • Search pattern
    ... Dim strfile As String ... Dim bAddressFound As Boolean ... Dim strCurrentChar As String ...
    (comp.databases.ms-access)
  • Auto Write Name and Merge across
    ... Dim Sheetname01 As String ... Dim WeekName01 As String ...
    (microsoft.public.excel.misc)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Help answer these 70-310 questions
    ... One argument is the string ... Dim output As New StringBuilder ... EmployeeLocations. ... You create a strongly named serviced component. ...
    (microsoft.public.cert.exam.mcsd)