ScottishTapWater
ScottishTapWater

Reputation: 4806

Initializing 2D Array

I'm trying to create an array whereby a 2 character code in each element corresponds to what pieces are where on a chessboard.However, I can't work out how do use the 2D arrays. I have the pieceArray declared as:

Public pieceArray(7,7) as String

And then I'm trying to populate it using this method:

pieceArray =      {"BR", "Bk", "BB", "BQ", "BK", "BB", "Bk", "BR",
                   "BP", "BP", "BP", "BP", "BP", "BP", "BP", "BP",
                   "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                   "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                   "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                   "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                   "WP", "WP", "WP", "WP", "WP", "WP", "WP", "WP",
                   "WR", "Wk", "WB", "WQ", "WK", "WB", "Wk", "WR"}

But this method gives me an error where the dimensions don't match up so could you please explain how to do this properly?

Thanks

Upvotes: 1

Views: 1540

Answers (1)

Stefan Falk
Stefan Falk

Reputation: 25397

Well, this is not how you access read/write data to a 2D array. It looks more like that:

' an array with 5 rows and 2 columns
Dim a(4,1) As Integer

Hard coding the way you are doing it would work like this:

Module arrayApl
   Sub Main()
      ' an array with 5 rows and 2 columns
      Dim a(,) As Integer = {{0, 0}, {1, 2}, {2, 4}, {3, 6}, {4, 8}}
      Dim i, j As Integer
      ' output each array element's value '
      For i = 0 To 4
          For j = 0 To 1
              Console.WriteLine("a[{0},{1}] = {2}", i, j, a(i, j))
          Next j
      Next i
      Console.ReadKey()
   End Sub
End Module

Maybe take a look at a tutorial.

Upvotes: 2

Related Questions