首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么数组在现有if语句之后变为null?

为什么数组在现有if语句之后变为null?
EN

Stack Overflow用户
提问于 2022-03-04 21:39:52
回答 1查看 112关注 0票数 0

我创建了一个带有未定义维度的映射数组,我在if语句中定义了这些维度。代码进入if并映射到我想要的值,直到它输出if并再次变为null。有人知道我做错了什么并能帮我解决吗?

这是我的代码:

代码语言:javascript
复制
        //Game settings
        const int CELL_SIZE = 70;
        int mapSize = 10;

        //Game Sprits
        Image white_Pawn;
        Image black_Pawn;

        Image white_Tiling;
        Image black_Tiling;
        Image Selected_Tiling;
        Image Path_Tiling;

        //Game variables
        int current_Player;
        bool isMoving;
        Button prevButton;
        Button pressedButton;

        int[,] map;
        Button[,] buttons;



        public Checkers_v2_0()
        {

            InitializeComponent();

            white_Pawn = new Bitmap(new Bitmap(@"..\..\Properties\Sprites\white_pawn.png"), new Size(CELL_SIZE - 10, CELL_SIZE - 10));
            black_Pawn = new Bitmap(new Bitmap(@"..\..\Properties\Sprites\black_pawn.png"), new Size(CELL_SIZE - 10, CELL_SIZE - 10));

            white_Tiling = new Bitmap(new Bitmap(@"..\..\Properties\Sprites\white_Tiling.png"), new Size(CELL_SIZE, CELL_SIZE));
            black_Tiling = new Bitmap(new Bitmap(@"..\..\Properties\Sprites\black_Tiling.png"), new Size(CELL_SIZE, CELL_SIZE));
            Selected_Tiling = new Bitmap(new Bitmap(@"..\..\Properties\Sprites\Selected_Tiling.png"), new Size(CELL_SIZE, CELL_SIZE));
            Path_Tiling = new Bitmap(new Bitmap(@"..\..\\Properties\Sprites\Path_Tiling.jpg"), new Size(CELL_SIZE, CELL_SIZE));

            this.Text = "Chechers";

            InitMenu();

            //ShowMenu();
            InitGame();
        }

        public void InitGame(bool player1type = false, bool player2type = false, int mapSize = 10, int timePerPlayer = 60)
        {
            current_Player = 1;
            isMoving = false;
            prevButton = null;
            Console.WriteLine(map);
            if (mapSize == 10)
            {
                Console.WriteLine("test OK");
                int[,] map = new int[12, 12]
                {
                            { 9,9,9,9,9,9,9,9,9,9,9,9 },
                            { 9,0,1,0,1,0,1,0,1,0,1,9 },
                            { 9,1,0,1,0,1,0,1,0,1,0,9 },    
                            { 9,0,1,0,1,0,1,0,1,0,1,9 },    
                            { 9,1,0,1,0,1,0,1,0,1,0,9 },    
                            { 9,0,0,0,0,0,0,0,0,0,0,9 },    
                            { 9,0,0,0,0,0,0,0,0,0,0,9 },    
                            { 9,0,2,0,2,0,2,0,2,0,2,9 },
                            { 9,2,0,2,0,2,0,2,0,2,0,9 },
                            { 9,0,2,0,2,0,2,0,2,0,2,9 },
                            { 9,2,0,2,0,2,0,2,0,2,0,9 },
                            { 9,9,9,9,9,9,9,9,9,9,9,9 }
                };
                Console.WriteLine(map[2, 1]);
                Button[,] buttons = new Button[10, 10];
            }
            else if (mapSize == 8)
            {
                int[,] map = new int[10, 10]
                {
                            { 9,9,9,9,9,9,9,9,9,9 },
                            { 9,0,1,0,1,0,1,0,1,9 },    
                            { 9,1,0,1,0,1,0,1,0,9 },    
                            { 9,0,1,0,1,0,1,0,1,9 },    
                            { 9,0,0,0,0,0,0,0,0,9 },    
                            { 9,0,0,0,0,0,0,0,0,9 },    
                            { 9,2,0,2,0,2,0,2,0,9 },
                            { 9,0,2,0,2,0,2,0,2,9 },
                            { 9,2,0,2,0,2,0,2,0,9 },
                            { 9,9,9,9,9,9,9,9,9,9 }
                };
                Button[,] buttons = new Button[8, 8];
            }
            else if (mapSize == 12)
            {
                int[,] map = new int[14, 14]
                {
                            { 9,9,9,9,9,9,9,9,9,9,9,9,9,9 },
                            { 9,0,1,0,1,0,1,0,1,0,1,0,1,9 },
                            { 9,1,0,1,0,1,0,1,0,1,0,1,0,9 },    
                            { 9,0,1,0,1,0,1,0,1,0,1,0,1,9 },    
                            { 9,1,0,1,0,1,0,1,0,1,0,1,0,9 },
                            { 9,0,1,0,1,0,1,0,1,0,1,0,1,9 },    
                            { 9,0,0,0,0,0,0,0,0,0,0,0,0,9 },    
                            { 9,0,0,0,0,0,0,0,0,0,0,0,0,9 },
                            { 9,2,0,2,0,2,0,2,0,2,0,0,0,9 },  
                            { 9,0,2,0,2,0,2,0,2,0,2,0,2,9 },
                            { 9,2,0,2,0,2,0,2,0,2,0,2,0,9 },
                            { 9,0,2,0,2,0,2,0,2,0,2,0,2,9 },
                            { 9,2,0,2,0,2,0,2,0,2,0,2,0,9 },
                            { 9,9,9,9,9,9,9,9,9,9,9,9,9,9 },
                };
                Button[,] buttons = new Button[12, 12];
            }
            Console.WriteLine(map[0, 0]);

            Create_Checkerboard(mapSize);
        }

这是输出:

代码语言:javascript
复制
test OK
1
Exception thrown: 'System.NullReferenceException' in Checkers_v2.0.exe
Object reference not set to an instance of an object.

我希望有:

代码语言:javascript
复制
test OK
1
9

溶液

--您已经声明了类实例的一个字段,以及各个作用域块中的局部变量。这些是不一样的。不要将局部变量声明为int,map =,只需执行map =。-LasseV.Karlsen

EN

回答 1

Stack Overflow用户

发布于 2022-03-05 07:52:45

我投票结束这个错误,但我想指出的是,有一件事应该让你更容易发现这个错误

典型的C#命名约定如下所示:

代码语言:javascript
复制
class ClassName{
  private string _variableName;

  public string PropertyName {get;set;}

  public void MethodName(string parameterName){
    string variableName;
  }
}

我们倾向于将在类级别声明的私有变量命名为前导下划线:_variableName

这在几个方面都有帮助;当我们在代码块中间从类头向下走了一千行时,可以看到:

代码语言:javascript
复制
_something = blahblah;

我们可以知道“这是一个类级变量”,而无需滚动以检查

如果我们看到一个接收数据赋值的PascalCase东西:

代码语言:javascript
复制
SomeThing = blahblah;

那是这个类的一个属性

如果我们看到没有前导下划线的东西:

代码语言:javascript
复制
something = blahblah;

它分配一个局部变量,声明在本地方法中的某个地方,或者它的参数列表

尽量避免将局部变量命名为类级变量;它们通常具有不同的用途,而跨名的机会可能表明,类级别1应该有一个更好的名称。

在任何情况下,如果您有一个_map和一个map,如果您已经编写了:

代码语言:javascript
复制
void SomeMethod(){

  string _map = ...

  ^^^^^^ ^^^^
   TYPE  NAME

也就是说,如果您在方法中编写了一个数据类型,后面跟着一个下划线,那么这是错误的;在string _map中,您定义了一个局部变量,而不是设置一个类级变量。

当您获得使用c#的经验时,这些事情开始成为潜在的问题--“噢,在方法中,在数据类型前面加上下划线变量;这是打破惯例的。”

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71357477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档