Solved

Dynamic multi-dimensional array

Posted on 2011-09-16
16
286 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get String split 5 48
Get Count in a variable with 5 digits always 1 28
VB.Net Serial COM1 Port - how to send this text via Serial COM port? 13 42
vb.net checkbox 7 41
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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now