Link to home
Start Free TrialLog in
Avatar of scbdpm
scbdpmFlag for United States of America

asked on

Dynamic multi-dimensional array

I am trying to use a multi-dimensional array where only one of the dimensions I will know at the start of the project.

I've defined it in a module as:
Public strXMLData(0, 5) As String

Within a function, I (try to) then populate the array. However, I'm getting an error on the line:

ReDim Preserve strXMLData(intXMLCount, 6)

intXMLCount is incrementing.

I feel very comfortable with single dimension arrays but seem to always stumble on multi....

HELP!
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

See ReDim:
http://msdn.microsoft.com/en-us/library/w8k3cys2(VS.71).aspx

    "When you use Preserve, you can resize only the last dimension of the array, and for every other dimension you must specify the same size it already has in the existing array."

So you either need to switch the order of your elements or approach the problem without using arrays.
*An alternative to arrays might be List of Lists.
SOLUTION
Avatar of strickdd
strickdd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of scbdpm

ASKER

I'm interested in using and learning about the 'List of Lists'.

however, when I use the code from above:
"Dim myList As New List(Of string())"

I get the following error:
"Error      10      Type 'List' is not defined.      "
There is a strange structure called an array of array that can do the thing.
Dim x(2)() As String
Dim a(1) As String
Dim b(2) As String

x(0) = a
x(1) = b

x(0)(0) = "Tutu"    'First element of array a - array a being in x(0)
x(0)(1) = "Toto"    'Second of array a
x(1)(0) = "Tata"    'First element of array b - array b being in x(1)

ReDim Preserve a(5)
ReDim Preserve b(6)
x(0) = a
x(1) = b

x(0)(5) = "Titi"    '6th element of array a
x(1)(6) = "Tintin"   '7th element of array b

Open in new window

What you used to call with x(1,6) becomes x(1)(6)
What version .Net are you working with?...
Avatar of scbdpm

ASKER

VS 2010
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
*Note we are using (0)(2) instead of (0,2).
Avatar of scbdpm

ASKER

ok, thanks

but I'm still getting an error at that first line of code:

"Dim myList As New List(Of string())"

I get the following error:
"Error      10      Type 'List' is not defined.      "
What kind of project did you start with?...it should be available automatically without needing to add any references.
Avatar of scbdpm

ASKER

an exe that was 'converted' from VS2003 to VS2010..????
That's why...Lists were introduced in 2005.  The converted project doesn't have the correct references.

I've found that converted projects tend to have a lot of issues like this...better to start with a NEW project directly in 2010 and then copy code over after rebuilding the UI.
Did you try importing the System.Collections.Generics namespace?
When you convert a project from an older version of Visual Studio, the target framework is usually not changed. So even if you are in Visual Studio 2010, you are probably set to work with an older version of the framework.

Go into the project's Properties windows, Compile tab, Advanced Compile Options, and set the target framework to a higher version, ideally version 4.

However since you skipped 3 versions of the framework by going straight from VS2003 to VS2010, you might end up with new errors for stuff that is not supported anymore. This depends on the classes you were using in the original. If switching to framework 4 gives you too many errors and you do not have the time to make the changes, lower the version.
Avatar of scbdpm

ASKER

all

i have to start by thanking all that participated in this question.

It's a bit tough to award points as I did gain some knowledge from each posting.

however, the two that I've awarded the points to, i did so as they helped ot answer the quesion I posted.

As I make my way to the VS2010 world, i'm sure I''ll have lots more questions.
i appreciate all the help that's out there!