Solved

Dynamic multi-dimensional array

Posted on 2011-09-16
16
275 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 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

18 Experts available now in Live!

Get 1:1 Help Now