简体   繁体   English

在C#中使用2d数组

[英]Using 2d array in C#

I'm getting the error: 我收到错误:

"Ambiguity between 'game.Form1.WallCheckerArray' and 'game.Form1.WallCheckerArray'" “'game.Form1.WallCheckerArray'和'game.Form1.WallCheckerArray'之间存在歧义”

Because I use it twice. 因为我用了两次。 Why can't I use the same array name twice with different values? 为什么我不能使用相同的数组名称两次使用不同的值? My code is below. 我的代码如下。 The array exists out of cordinates. 该数组存在于cordinates之外。

my array: 我的阵列:

private int[,] WallCheckerArray = new int[28, 4];// <<----- was the problem
        int[,] WallCheckerArray = {
                                      {220,250,13,64},//1
                                      {24,58,24,55},//2
                                      {104,206,22,55},//3
                                      {264,370,22,55},//4
                                      {382,450,22,55},//5
                                      {24,92,74,109},//6
                                      {104,136,74,185},//7
                                      {136,206,114,138},//8
                                      {150,326,74,98},//9
                                      {225,255,98,140},//10
                                      {345,365,74,185},//11
                                      {275,355,114,138},//12
                                      {384,445,74,109},//13
                                      {104,136,200,270},//14
                                      {150,330,240,270},//15
                                      {225,255,270,315},//16
                                      {340,370,200,270},//17
                                      {20,85,285,305},//18
                                      {50,85,305,345},//19
                                      {104,214,285,315},//20
                                      {274,368,285,315},//21
                                      {378,445,285,305},//22
                                      {378,415,305,345},//23
                                      {24,195,365,375},//24
                                      {104,154,335,375},//25
                                      {165,339,335,345},//26
                                      {215,245,335,375},//27
                                      {265,445,365,375},//28
                                      {355,365,335,375}//29
                                  };


int i = 0;
for( i = 0; i < 29; i++)
{
    if (((x >= WallCheckerArray[i, 0] && x <= WallCheckerArray[i, 1]) && (y >= WallCheckerArray[i, 2] && y <= WallCheckerArray[i, 3])))//-----------------------------------}-
         {
            InsideWC();
            System.Console.WriteLine(WallCheckerArray[i, 1]);
         }
}

A line of code like int[,] WallCheckerArray = something is read by the compiler as "Make a new int[,] named WallCheckerArray. And the problem is, you do that twice in a row; you can't have two different variables with the same name. If you don't give a variable an access modifier, the C# compiler will assume you wanted it to be Private . 一行代码如int[,] WallCheckerArray = something被编译器读作“Make a new int[,]命名为WallCheckerArray。问题是,你连续两次这样做;你不能有两个不同的变量如果你没有给变量一个访问修饰符,C#编译器会认为你想要它是Private

To make your code work: 要使代码工作:

private int[,] WallCheckerArray = new int[28, 4];
        WallCheckerArray = {
                                 /* Your Data */
                           };

FYI: you can actually make your array and fill it with data in the same statement, like so: 仅供参考:您实际上可以在同一个语句中创建数组并用数据填充它,如下所示:

private int[,] WallCheckerArray = {
                                      {220,250,13,64},
                                      {24,58,24,55},
                                      {104,206,22,55},
                                      // and so on
                                  };

This way, you don't have to worry about declaring the size of the array, it'll be handled for you. 这样,您不必担心声明数组的大小,它将为您处理。

不得不删除“private int [,] WallCheckerArray = new int [28,4];”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM