0

Alright, so I'm used to PHP where I can declare a multi-level array like $something[0][1] = "test";. I need to be able to accomplish the same thing, but I'm using VB.NET. How would I do this?

And sorry if this isn't what a multi-dimentional array is, I might be wrong at what it's called but that's what I want to do.

Thanks!

2
  • If you have used PHP before, it may be better to use C# instead of VB.NET
    – Alvin Wong
    Jun 21, 2012 at 6:15
  • @BigYellowCactus The syntax of PHP is more similar to C# than VB.NET
    – Alvin Wong
    Jun 21, 2012 at 7:46

2 Answers 2

1

Multidimensional array in VB.Net...

Dim twoDimensionalArray(10, 10) As String
twoDimensionalArray(0, 1) = "test"

I rarely use arrays, however. More elegant solutions can typically be achieved using Lists, Dictionaries, or combinations of the two.

Update .

The (10, 10) is the upper bound of the array (the size is actually 11, 0 through 10). If you don't specify the bounds, you have to Redim Preserve the array when you want to add to it. That's one good thing about lists, you don't have to specify an initial size and you can add to them freely.

Here's a quick example of a list of lists.

Dim listOfLists As New List(Of List(Of String))
listOfLists.Add(New List(Of String)(New String() {"a", "b", "c"}))
listOfLists.Add(New List(Of String)(New String() {"d", "e", "f"}))
listOfLists.Add(New List(Of String)(New String() {"g", "h", "i"}))
'listOfLists(0)(0) = "a"
'listOfLists(0)(1) = "b"
'listOfLists(2)(1) = "h"
3
  • I would assume that the (10, 10) is the amount of items in that array. Is there a way to do it without specifying that? Also then could you explain briefly how to achieve my desired result with Lists?
    – DannyF247
    Jun 21, 2012 at 6:29
  • 1
    @DannyF247 List is usually a single dimensional array containing objects. since .net 2.0 where generics were introduced you can create a List with a specified Type (e.g. List(Of T), you can even store Lists in a list, if you like (e.g.List(Of List(Of String)) (I however haven't encountered a situation yet where this would have been needed)
    – Nicholas
    Jun 21, 2012 at 8:39
  • No, the 10, 10 is the upper bound of the array, not the array size. You have actually created an 11 x 11 array there. Jun 21, 2012 at 14:44
0

Just a plain sample with dynamic resizing of the array

Dim arr(0)() As String            '** array declaration 
For i As Integer = 0 To 100       '** Outer loop (for the 1st dimension)
   For j As Integer = 0 To 1      '** inner loop (for the 2nd dimension)
      ReDim Preserve arr(i)       '** Resize the first dimension array preserving the stored values
      ReDim Preserve arr(i)(j)    '** Resize the 2nd dimension array preserving the stored values
      arr(i)(j) = String.Format("I={0},J={1}", i, j) '** Store a value 
   Next                           
Next

In .NET Arrays are usually static and won't be automatically resized. (As for example in Javascript etc.) Therefore it's necessary to manually resize the array each time you want to add a new item, or specify the size at the beginning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.