How to dynamically resize an array?

  • Thread starter Thread starter roidy
  • Start date Start date
R

roidy

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
 
roidy said:
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?

Put the caret on Redim and press F1.

The better alternative is to use a List(Of) or an ArrayList.


Armin
 
roidy said:
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?

Put the caret on Redim and press F1.

The better alternative is to use a List(Of) or an ArrayList.


Armin
 
roidy said:
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?

Use a 'System.Collections.Specialized.StringCollection' or 'List(Of String)'
instead of the array.

Arrays can be resized using 'ReDim Preserve', but be aware that this will
create a new array and copy the values from the existing array to the newly
created array. Consenquently the performance of 'ReDim Preserve' is bad.
 
roidy said:
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?

Use a 'System.Collections.Specialized.StringCollection' or 'List(Of String)'
instead of the array.

Arrays can be resized using 'ReDim Preserve', but be aware that this will
create a new array and copy the values from the existing array to the newly
created array. Consenquently the performance of 'ReDim Preserve' is bad.
 
Thanks guys, Yep I`m stupid, it`s right there in the help file 'Preserve'
don`t know how I missed that. Since I only need to resize the array once
during Form.Load Preserve will be fine and easier to write into the code
I`ve already written.

Rob
 
Thanks guys, Yep I`m stupid, it`s right there in the help file 'Preserve'
don`t know how I missed that. Since I only need to resize the array once
during Form.Load Preserve will be fine and easier to write into the code
I`ve already written.

Rob
 
Roidy,

There is no need to change this everywhere in your program, you can use it
although everywhere, however keep in mind that VB6 like programs are in
general 10 times slower then newer ones.

There are simple tricks to use with converted programs like putting option
strict on. However, things like the redim stays slow.

Therefore as you are using new arrays in your programs, then simply start
with using the arraylist. It is simpler to use then an array.

Cor

roidy said:
Thanks guys, Yep I`m stupid, it`s right there in the help file 'Preserve'
don`t know how I missed that. Since I only need to resize the array once
during Form.Load Preserve will be fine and easier to write into the code
I`ve already written.

Rob
 
Roidy,

There is no need to change this everywhere in your program, you can use it
although everywhere, however keep in mind that VB6 like programs are in
general 10 times slower then newer ones.

There are simple tricks to use with converted programs like putting option
strict on. However, things like the redim stays slow.

Therefore as you are using new arrays in your programs, then simply start
with using the arraylist. It is simpler to use then an array.

Cor

roidy said:
Thanks guys, Yep I`m stupid, it`s right there in the help file 'Preserve'
don`t know how I missed that. Since I only need to resize the array once
during Form.Load Preserve will be fine and easier to write into the code
I`ve already written.

Rob
 
roidy said:
Thanks guys, Yep I`m stupid, it`s right there in the help file 'Preserve'
don`t know how I missed that. Since I only need to resize the array once
during Form.Load Preserve will be fine and easier to write into the code
I`ve already written.

Well, for sure it's easier to add the 'Preserve' keyword, but note that
resizing the array in every iteration of the loop will reduce the
performance significantly compared to 'StringCollection' or 'List(Of
String)'. BTW: I would not use 'ArrayList' because it's not
strongly-typed, which means that you can add objects/values of arbitrary
types to the list.
 
roidy said:
Thanks guys, Yep I`m stupid, it`s right there in the help file 'Preserve'
don`t know how I missed that. Since I only need to resize the array once
during Form.Load Preserve will be fine and easier to write into the code
I`ve already written.

Well, for sure it's easier to add the 'Preserve' keyword, but note that
resizing the array in every iteration of the loop will reduce the
performance significantly compared to 'StringCollection' or 'List(Of
String)'. BTW: I would not use 'ArrayList' because it's not
strongly-typed, which means that you can add objects/values of arbitrary
types to the list.
 
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
 
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
 
Mike said:
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


Have a look at File.ReadAllLines(filename).
 
Mike said:
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


Have a look at File.ReadAllLines(filename).
 
Family said:
Have a look at File.ReadAllLines(filename).

I saw that. I need to tear down my C/C++ class chart and hang up the
one for .net :-) Do you know if .NET support memory file maps? I did
a quick lookup and didn't see it.

Yeah Rob, its just one line, but you should have a catch:

Function LoadStringList(ByVal fn As String) As String()
Try
Return File.ReadAllLines(fn)
Catch ex As Exception
' oops some error
'Console.WriteLine("{0}", ex.Message)
End Try
Return Nothing
End Function

--
 
Family said:
Have a look at File.ReadAllLines(filename).

I saw that. I need to tear down my C/C++ class chart and hang up the
one for .net :-) Do you know if .NET support memory file maps? I did
a quick lookup and didn't see it.

Yeah Rob, its just one line, but you should have a catch:

Function LoadStringList(ByVal fn As String) As String()
Try
Return File.ReadAllLines(fn)
Catch ex As Exception
' oops some error
'Console.WriteLine("{0}", ex.Message)
End Try
Return Nothing
End Function

--
 
There is no need to change this everywhere in your program,
you can use it although everywhere, however keep in mind
that VB6 like programs are in general 10 times slower then
newer ones.

You're talking a load of bollocks, Ligthert!
 
There is no need to change this everywhere in your program,
you can use it although everywhere, however keep in mind
that VB6 like programs are in general 10 times slower then
newer ones.

You're talking a load of bollocks, Ligthert!
 
Herfried K. Wagner said:
Well, for sure it's easier to add the 'Preserve' keyword, but
note that resizing the array in every iteration of the loop
will reduce the performance significantly . . .

Are you a complete dick head, Wagner? The man said that he will resize the
array once! Besides, even if he does want to resize it more than once he
very definitely never said that he was goiung to resize it at every
iteration of the loop. Once again you're making things up and putting words
into other people's mouths simply so that you can sound off and make
yourself feel more important than you actually are.
 
Back
Top