i have the following array:

int[,] simpleArray =new int[,]{
              {1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8},{1,9},{1,10},{1,11} ,
              {2,1},                                                                {2,11},
              {3,1},                                                                   {3,11},
              {4,1},                                                                 {4,11},
              {5,1},                                                                {5,11},
              {6,1},                                                                  {6,11},
              {7,1},                                                                 {7,11},
              {8,1},                                                                 {8,11},
              {9,1},                                                                 {9,11},
              {10,1},                                                                {10,11},
              {11,1},{11,2},{11,3},{11,4},{11,5},{11,6},{11,7},{11,8},{11,9},{11,10},{11,11},

                                };

            for (int i = 0; i < 11; i++)
            {
                for (int m = 0; m < 11; m++)
                {
                    if (simpleArray[i, m] == simpleArray[1, 1])
                    {
                        string one = "";
                        string two = "";
                        string three = "Go To Jail";
                        string four = "";
                        string five = "";

                        Console.WriteLine(one);
                        Console.WriteLine(two);
                        Console.WriteLine(three);
                        Console.WriteLine(four);
                        Console.WriteLine(five);

                        Console.WriteLine(i);
                        Console.WriteLine(m);

                    }
                    if (simpleArray[i, m] == simpleArray[1, 2]) // array index out of bound!!? why?
                    {
                        string one = "";
                        string two = "";
                        string three = "Go To Jail";
                        string four = "";
                        string five = "";

                        Console.WriteLine(one);
                        Console.WriteLine(two);
                        Console.WriteLine(three);
                        Console.WriteLine(four);
                        Console.WriteLine(five);
                    }

i try to create a monopoly board. it gives me array index out of bound on the second if. why?

Recommended Answers

All 4 Replies

In this code simpleArray[i, m] == simpleArray[1, 2]
there is no array of [1, 2]!
but I dont even know what are you trying to do. maybe would help, if you explain us your code a bit better.

Mitja

New Order,
Just out of curiosity, why are you trying to use a string array for this?

It seems that making a space object and an array or list of spaces would be a much more simple and intuitive solution.

Unless you absolutely have to, I would advise against the approach you are currently using.

If you do need to, you may look at using a jagged array instead, creating an array such as string[][] will create an array of arrays which will probably fit your needs a little better. check out this msdn page for an idea of how they work http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

i know i could use a string array. but i tried to play with arrays a bit , i want to build a monopoly board. the empty places , 0 should print nothing and the places filled with numbers (other than 0) should print you a particular square.

why does it give me index out of bound. hmmmm, i dont completely understand the syntax, did i create one row of an array?

New Order,

The first index specifies which set of the arrays is being referenced, the second specifies which item in the set.

so, if you have an array {{1,2},{3,4},{7,12}}

and reference [1,1], you are referencing the second set (0 based of course), and the second item in the set so 4;

so when you are asking your program to reference [1,2], it is trying to look at index 3 of the second item, {1,2}. There is no third item for it to reference, [1,0] and [1,1] would work fine, but not [1,2]. Make sense?

To iterate a little further, your array's explicit definition is [40,2]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.