Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

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!
0
scbdpm
Asked:
scbdpm
  • 6
  • 5
  • 3
  • +1
3 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
strickddCommented:
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
 
scbdpmAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jacques Bourgeois (James Burger)PresidentCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What version .Net are you working with?...
0
 
scbdpmAuthor Commented:
VS 2010
0
 
strickddCommented:
Add an Imports statement and import System.Collections.Generic

Imports System.Collections.Generic

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
...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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*Note we are using (0)(2) instead of (0,2).
0
 
scbdpmAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What kind of project did you start with?...it should be available automatically without needing to add any references.
0
 
scbdpmAuthor Commented:
an exe that was 'converted' from VS2003 to VS2010..????
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
strickddCommented:
Did you try importing the System.Collections.Generics namespace?
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
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
 
scbdpmAuthor Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now