Skip to main content
 首页 » 编程设计

c#之在 C# 中使用二维数组

2024年02月13日15Renyi-Fan

我收到错误:

"Ambiguity between 'game.Form1.WallCheckerArray' and 'game.Form1.WallCheckerArray'"

因为我用了两次。为什么我不能两次使用相同的数组名称和不同的值?我的代码如下。该数组存在于坐标之外。

我的数组:

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]); 
         } 
} 

请您参考如下方法:

一行代码,如 int[,] WallCheckerArray = something 被编译器读取为“创建一个名为 WallCheckerArray 的新 int[,]。问题是,你连续两次这样做;你不能有两个同名的不同变量。如果你没有给变量一个访问修饰符,C# 编译器会假设你希望它是 Private.

要使您的代码正常工作:

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

仅供引用:您实际上可以在同一语句中创建数组并用数据填充它,如下所示:

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

这样,您不必担心声明数组的大小,它会为您处理。