Solved

Dynamic multi-dimensional array

Posted on 2011-09-16
16
292 Views
Last Modified: 2012-05-12
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!
0
Comment
Question by:scbdpm
  • 6
  • 5
  • 3
  • +1
16 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36549129
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.
0
 
LVL 28

Assisted Solution

by:strickdd
strickdd earned 375 total points
ID: 36549207
I don't know if you are able to change the structure much, but you could always do a list of arrays:

Dim myList As New List(Of string())

myList.Add({"dog", "cat", "fish"})
myList.Add({"cone"})
myList.Add({"someVal", "otherVal"})

for each itemArr As string() in myList
    for i as Integer = 0 to itemArr.Length - 1
          'itemArr(i)
    next i
next

Open in new window

0
 

Author Comment

by:scbdpm
ID: 36549614
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.      "
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 40
ID: 36549678
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)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36549711
What version .Net are you working with?...
0
 

Author Comment

by:scbdpm
ID: 36549817
VS 2010
0
 
LVL 28

Assisted Solution

by:strickdd
strickdd earned 375 total points
ID: 36549832
Add an Imports statement and import System.Collections.Generic

Imports System.Collections.Generic

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 36549877
...and you can literally have a List of Lists (as opposed to a List of Array):

    Dim myList As New List(Of List(Of String))

You'd have to create a list first to place into the List of Lists:

    Dim Pets as New List(Of String)
    Pets.Add("Cat")
    Pets.Add("Dog")
    Pets.Add("Fish")

Now add it to myList:

    myList.Add(Pets)

Example of accessing it:

    MessageBox.Show(myList(0)(2)) ' <-- should be "Fish"
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36549880
*Note we are using (0)(2) instead of (0,2).
0
 

Author Comment

by:scbdpm
ID: 36549907
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.      "
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36549923
What kind of project did you start with?...it should be available automatically without needing to add any references.
0
 

Author Comment

by:scbdpm
ID: 36549937
an exe that was 'converted' from VS2003 to VS2010..????
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36549951
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.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36550388
Did you try importing the System.Collections.Generics namespace?
0
 
LVL 40
ID: 36550609
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.
0
 

Author Comment

by:scbdpm
ID: 36555302
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!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question