Redim array VB6 to dotnet

A

Adrian

Hi
In VB 6 I would declare an array in the general part to make it visible to
all parts then once I know how elements I had I would redim it with the
amount thus

Dim testarray() as string

.......
.......

Redim testarray(9) as string

How do I do this in VB.Net?

Thanks
 
C

Chris, Master of All Things Insignificant

Easiest way to do this in VB.NET is to use an ArrayList object. This is an
array that has an Add method so you don't have to redim it. It grows as you
needed. Otherwise, you can just use the ReDim keyword like your use to.

Dim S() as String
ReDim S(9)

Chris
 
H

Herfried K. Wagner [MVP]

Adrian said:
In VB 6 I would declare an array in the general part to make it visible to
all parts then once I know how elements I had I would redim it with the
amount thus

Dim testarray() as string

......
......

Redim testarray(9) as string

How do I do this in VB.Net?

It works the same, but you'll have to remove the 'As String' on the 'ReDim'
statement. Notice that 9 is treated as the upper-bound of the new array, so
the array will hold 10 items with indices 0 through 9.
 
B

Brian Patterson

Also keep in mind that when you redim the array - you will lose the contents
unless you use the preserve keyword to maintain the contents. You may want
to look into using the ArrayList class rather than using a standard array
since this grows as needed plus you can sort and so on.

Brian Patterson
http://dotnet.redeyepos.com
 
H

Herfried K. Wagner [MVP]

Brian Patterson said:
Also keep in mind that when you redim the array - you will lose the
contents unless you use the preserve keyword to maintain the contents.
You may want to look into using the ArrayList class rather than using a
standard array since this grows as needed plus you can sort and so on.

I didn't specifically mention 'Preserve' because the behavior didn't change
between VB6 and VB.NET. You can easily sort normal arrays too using
'Array.Sort' too. Arrays give you easy-to-use type-safety, arraylists
don't. If the number of items stored in the array rarely changes, using an
array is the preferred way. Alternatively, implementing a type-safe
collection or waiting for generic collection classes of Whidbey may be an
option if type-safety is a requirement and you want to get rid of tons of
'DirectCast' statements in your code.
 
A

Adrian

Many thanks to you all, its been of great help.

Herfried K. Wagner said:
I didn't specifically mention 'Preserve' because the behavior didn't
change between VB6 and VB.NET. You can easily sort normal arrays too
using 'Array.Sort' too. Arrays give you easy-to-use type-safety,
arraylists don't. If the number of items stored in the array rarely
changes, using an array is the preferred way. Alternatively, implementing
a type-safe collection or waiting for generic collection classes of
Whidbey may be an option if type-safety is a requirement and you want to
get rid of tons of 'DirectCast' statements in your code.
 

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