首页 > 解决方案 > 对象 C# 的二维数组

问题描述

我知道,这个问题似乎被问了很多,但我仍然无法使用人们提供的解决方案来解决它。因此,在尝试使用二维对象数组时,我也遇到了异常“System.NullReferenceException:'对象引用未设置为对象的实例。'”。所以这是不工作的代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Console_multi_fonctionnelle_basique
    {
        partial class Program
        {
             public class SudokuSolver
             {
                 //Initialisation code
                 public SudokuSolver()
                 {
                     GridValue[,] SudokuGrid = new GridValue[9, 9];
                     SudokuDisplay(SudokuGrid);
                     Console.ReadKey();
                }
                //Store values for every slot
                class GridValue
                {
                    public bool CanBe1 { get; set; } = false;
                    public bool CanBe2 { get; set; } = false;
                    public bool CanBe3 { get; set; } = false;
                    public bool CanBe4 { get; set; } = false;
                    public bool CanBe5 { get; set; } = false;
                    public bool CanBe6 { get; set; } = false;
                    public bool CanBe7 { get; set; } = false;
                    public bool CanBe8 { get; set; } = false;
                    public bool CanBe9 { get; set; } = false;
                    public bool AlreadySolved { get; set; } = false;
                    public int Value { get; set; } = 0;
                }
                //Display the grid
                void SudokuDisplay(GridValue[,] Sudoku)
                {
                    Sudoku[1, 1].Value = 1;
                    Sudoku[1, 2].Value = 2;
                    Console.WriteLine(Sudoku[0, 0].Value + Sudoku[1,0].Value);
                }
                //To Do
                //Ask the values for every slot
                //Verify if the slot can contain a number
                //Choose the most appropriated number for the slot
                //End the program after user pressing an key
            }
        }
    }

因此,如您所见,我想使用 9x9 大小的“GridValue”类的 2D 数组,以便每个“Grid slot”都有自己的变量,以便以后解决数独问题。但是程序看起来不理解我的数组包含“GridValue”对象所以他似乎不理解每个数组值都包含多个变量......所以我想要的结果是我可以为其中一个对象定义变量数组没有异常。

标签: c#arraysclassobjectmultidimensional-array

解决方案


您创建了一个空数组,因此当您尝试访问一个元素时,会引发异常。初始化数组的元素,你应该没问题。

public class SudokuSolver
{
    //Initialisation code
    public SudokuSolver()
    {
        GridValue[,] SudokuGrid = new GridValue[9, 9];

    //###################

        for (int r=0; r < 9; r++) 
        { 
            for (int c = 0; c < 9; c++) 
            { 
                SudokuGrid[r, c] = new GridValue(); 
            } 
        }

    //###################

        SudokuDisplay(SudokuGrid);
        Console.ReadKey();
    }
    //Store values for every slot
    class GridValue
    {
        public bool CanBe1 { get; set; } = false;
        public bool CanBe2 { get; set; } = false;
        public bool CanBe3 { get; set; } = false;
        public bool CanBe4 { get; set; } = false;
        public bool CanBe5 { get; set; } = false;
        public bool CanBe6 { get; set; } = false;
        public bool CanBe7 { get; set; } = false;
        public bool CanBe8 { get; set; } = false;
        public bool CanBe9 { get; set; } = false;
        public bool AlreadySolved { get; set; } = false;
        public int Value { get; set; } = 0;
    }
    //Display the grid
    void SudokuDisplay(GridValue[,] Sudoku)
    {
        Sudoku[1, 1].Value = 1;
        Sudoku[1, 2].Value = 2;
        Console.WriteLine(Sudoku[0, 0].Value + Sudoku[1, 0].Value);
    }
    //To Do
    //Ask the values for every slot
    //Verify if the slot can contain a number
    //Choose the most appropriated number for the slot
    //End the program after user pressing an key
}

推荐阅读