alternative to ReDim trick possible in VB6

J

John A Grandy

In VB6 you could get away with the following code:


Dim Index As Integer
Dim ItemsCount As Integer
Dim StringArray() As String
Dim StringValue As String


'....

'at runtime , ItemsCount is determined from a database query, or a
user-entry, etc.

'....

For Index = 0 To ItemsCount - 1

'each StringValue is determined at runtime from a database query, or a
user-entry, etc.

ReDim StringArray(0 To Index) As String
StringArray(Index) = StringValue

Next Index


However, you can't do this in VB.NET ... (compile-time error on ReDim)

Does anyone know an alternative coding method ?

Thanks.
 
I

Imran Koradia

You can still use ReDim - but there are some restrictions. First, VB.NET
(atleast in version 2002 and 2003) does not support the use of lower bound
when defining an array - by default, the lower bound is always 0. So, you
cannot do Redim StringArray(0 To Index); instead, just use Redim
StringArray(Index). Secondly, ReDim does not allow changing the type of the
array. Hence, Redim(..) As String is also illegal in VB.NET in versions 2002
and 2003. So, your syntax for Redim should look like:

ReDim StringArray(Index)

hope that helps..
Imran.
 
G

Guest

John:

I don't understand. I have the following code that works fine:

Dim sKey() As String

For i = 0 to itemcount
ReDim Preserve sKey(liCounter)
skey = "some value"
Next i

What am I missing?

Venkat
 
J

John A Grandy

but aren't you ReDim'ing your array as an array of Objects , not Strings ?

I think that's the key. For Arrays of Objects, dynamic ReDim works
similarly to VB6.
 
K

Kejpa

No!
Redim changes the dimension of the array, not the type. The type is
determined in the declaration of the variable
Dim sKey() As String ' the name sKey is an array of strings, the number is
undetermined
Redim sKey(8) ' the array known as sKey contains 9 strings

HTH
/Kejpa
 
C

Cor Ligthert

John,

My advise is standard in this, use the redim only for upgrade purpose and as
soon as you have to use a dynamic array go to the arraylist. The only thing
you miss in that is in my opinion the sort and when you do not need that is
the arraylist much easier to use and has for sure much more performance.

Just my thought,

Cor

"John A Grandy"
..
 
G

Guest

Hi John

The way I like to do this avoids ReDim, although the syntax is a little ugly:

Dim i As Integer
Dim strings() As String = New String(i) {}

HTH

Nigel
 
H

Herfried K. Wagner [MVP]

Nigel Armstrong said:
The way I like to do this avoids ReDim, although the syntax is a little
ugly:

Dim i As Integer
Dim strings() As String = New String(i) {}

There are no advantages over 'ReDim' with this solution...
 
G

Guest

Hi Herfried

Indeed not, the IL code is indentical...I just never liked ReDim! I prefer
the declare and initialize in a single statement approach...

Nigel

VB code:
1.
Dim i As Integer = 11
Dim strings() As String = New String(i) {}
2.
Dim i As Integer = 11
Dim strings() As String
ReDim strings(i)

IL generated:
1.
IL_0000: nop
IL_0001: ldc.i4.s 11
IL_0003: stloc.0
IL_0004: ldloc.0
IL_0005: ldc.i4.1
IL_0006: add.ovf
IL_0007: newarr [mscorlib]System.String
IL_000c: stloc.1
IL_000d: nop
IL_000e: ret

2.
IL_0000: nop
IL_0001: ldc.i4.s 11
IL_0003: stloc.0
IL_0004: ldloc.0
IL_0005: ldc.i4.1
IL_0006: add.ovf
IL_0007: newarr [mscorlib]System.String
IL_000c: stloc.1
IL_000d: nop
IL_000e: ret
 
J

Jay B. Harlow [MVP - Outlook]

Nigel,
Then why not:
3.
Dim i As Integer = 11
Dim strings(i) As String

Which is "short & sweet"! As you suggest ReDim is two statements, while "New
String(i) {}" is "syntax is a little ugly".

I tend to prefer #3 first, #2 second, followed by #1. I do however use #1
first if there are actually a list of values being supplied.

Dim strings() As String = New String() {"first", "second","third",
"fourth"}

Or simply:

Dim strings() As String = {"first", "second","third", "fourth"}

Of course the major problem with array definitions in .NET is the "off by
one" problem, your array really has 12 items in it, not 11 as some might
think the definition suggests...

Hope this helps
Jay

Nigel Armstrong said:
Hi Herfried

Indeed not, the IL code is indentical...I just never liked ReDim! I prefer
the declare and initialize in a single statement approach...

Nigel

VB code:
1.
Dim i As Integer = 11
Dim strings() As String = New String(i) {}
2.
Dim i As Integer = 11
Dim strings() As String
ReDim strings(i)

IL generated:
1.
IL_0000: nop
IL_0001: ldc.i4.s 11
IL_0003: stloc.0
IL_0004: ldloc.0
IL_0005: ldc.i4.1
IL_0006: add.ovf
IL_0007: newarr [mscorlib]System.String
IL_000c: stloc.1
IL_000d: nop
IL_000e: ret

2.
IL_0000: nop
IL_0001: ldc.i4.s 11
IL_0003: stloc.0
IL_0004: ldloc.0
IL_0005: ldc.i4.1
IL_0006: add.ovf
IL_0007: newarr [mscorlib]System.String
IL_000c: stloc.1
IL_000d: nop
IL_000e: ret

Herfried K. Wagner said:
There are no advantages over 'ReDim' with this solution...
 

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