Dynamic arrays

brainleak

Well-known member
Joined
Dec 20, 2016
Messages
54
Location
Barcelona area
Programming Experience
3-5
Hello, this is my very first post here.

I have been using VB6 for the last few years and I have recently decided to move on to .Net so I'm trying to learn the new features.

In VB6 I would sometimes use the ReDim statement for 2 dimensional arrays like this:

VB.NET:
Dim myVar As Double
'...
nCols = 8
nRows = 5
'...
ReDim MyVar(nRows, nCols)
'...

but this doesn't work in vb.net so, what would be a way around?
 
If you want that variable to refer to a 2D array of Double then you have to declare it as that type in the first place.
Dim myVar As Double(,)
'...
nCols = 8
nRows = 5
'...
ReDim MyVar(nRows, nCols)
'...
Also, keep in mind that arrays in .NET are zero-based, i.e. the first element is at index 0, and that explicitly sizing arrays is done by specifying the upper bound, i.e. the index of the last element. The upper bound is always 1 less than the length. That means that code above creates an array that is 9x6 rather than 8x5.
 
Actually I meant to write

Dim myVar As Double()

but then I forgot about the data type.

So the difference with VB6 is the colon within the parentheses:

Dim myVar As Double(,)

Thank you!

If you want that variable to refer to a 2D array of Double then you have to declare it as that type in the first place.
Dim myVar As Double(,)
'...
nCols = 8
nRows = 5
'...
ReDim MyVar(nRows, nCols)
'...
Also, keep in mind that arrays in .NET are zero-based, i.e. the first element is at index 0, and that explicitly sizing arrays is done by specifying the upper bound, i.e. the index of the last element. The upper bound is always 1 less than the length. That means that code above creates an array that is 9x6 rather than 8x5.
 
Back
Top