How to dynamically resize an array?

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
 
A

Armin Zingler

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
 
A

Armin Zingler

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
 
H

Herfried K. Wagner [MVP]

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.
 
H

Herfried K. Wagner [MVP]

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.
 
R

roidy

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
 
R

roidy

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
 
C

Cor Ligthert[MVP]

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
 
C

Cor Ligthert[MVP]

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
 
H

Herfried K. Wagner [MVP]

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.
 
H

Herfried K. Wagner [MVP]

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.
 
M

Mike

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
 
M

Mike

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
 
F

Family Tree Mike

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).
 
F

Family Tree Mike

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).
 
M

Mike

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

--
 
M

Mike

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

--
 
M

Michael Williams

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!
 
M

Michael Williams

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!
 
M

Michael Williams

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top