Connect with Expertise | Find Experts, Get Answers, Share Insights

Redim array VB6 to dotnet

Adrian
 
Posts: n/a
#1: Nov 21 '05
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



Chris, Master of All Things Insignificant
 
Posts: n/a
#2: Nov 21 '05

re: Redim array VB6 to dotnet


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


"Adrian" <Adrian@nospamhotmail.com.uk> wrote in message
news:csk211$dcq$1@hercules.btinternet.com...[color=blue]
> 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
>[/color]


Herfried K. Wagner [MVP]
 
Posts: n/a
#3: Nov 21 '05

re: Redim array VB6 to dotnet


"Adrian" <Adrian@nospamhotmail.com.uk> schrieb:[color=blue]
> 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?[/color]

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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Brian Patterson
 
Posts: n/a
#4: Nov 21 '05

re: Redim array VB6 to dotnet


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

"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:es0CvAb$EHA.3372@TK2MSFTNGP10.phx.gbl...[color=blue]
> "Adrian" <Adrian@nospamhotmail.com.uk> schrieb:[color=green]
>> 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?[/color]
>
> 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.
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>[/color]


Herfried K. Wagner [MVP]
 
Posts: n/a
#5: Nov 21 '05

re: Redim array VB6 to dotnet


"Brian Patterson" <briandpattersonSPAM_NO@mchsi.com> schrieb:[color=blue]
> 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.[/color]

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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Adrian
 
Posts: n/a
#6: Nov 21 '05

re: Redim array VB6 to dotnet


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

"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:OEamCqb$EHA.3616@TK2MSFTNGP11.phx.gbl...[color=blue]
> "Brian Patterson" <briandpattersonSPAM_NO@mchsi.com> schrieb:[color=green]
>> 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.[/color]
>
> 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.
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>[/color]


Cor Ligthert
 
Posts: n/a
#7: Nov 21 '05

re: Redim array VB6 to dotnet


Adrian,

I completly agree with Chris and Brian,

Maybe that helps

Cor


Closed Thread

Tags
vb6 redim