簡體   English   中英

對象C#的2D數組

[英]2D array of object C#

我知道,這個問題似乎問了很多,但仍然無法使用人們提供的解決方案來解決。 因此,在嘗試使用2D對象數組時,我也遇到了異常“ 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的2D數組“ GridValue”類,以便每個“ Grid插槽”都有自己的變量,以供以后解決數獨問題。 但是程序看起來好像不了解我的Array包含“ GridValue”對象,因此他似乎不了解每個Array值都包含多個變量...所以我想要的結果是我可以為其中的一個對象定義變量數組而不會出現異常。

我希望這個例子可以幫助您理解OOP。 歡迎來到社區!

using System;

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            SudokuSolver sudokuSolver = new SudokuSolver(9, 9);

            sudokuSolver.DisplayGrid();

            Console.ReadKey();
        }
    } 


    public class SudokuSolver
    {
        //Store every values needed for every "slot"
        public 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;
        }

        public GridValue[,] SudokuGrid { get; }

        //Code d'initialisation
        public SudokuSolver(int x, int y)
        { 
            // only initialize in the constructor, no calls
            SudokuGrid = new GridValue[x, y]; // the array exists now in mem, but each entry is pointing to null
            for (int i = 0; i < x; i++)
            {
                for (int j = 0; j < y; j++)
                {
                    SudokuGrid[i, j] = new GridValue();
                }
            }
        }



        //Display the grid => then name it that way!
        public void DisplayGrid()
        {
            SudokuGrid[1, 1].Value = 1;
            SudokuGrid[1, 2].Value = 2;
            Console.WriteLine(SudokuGrid[0, 0].Value + SudokuGrid[1, 0].Value);
        }


     }

}

您創建了一個空數組,因此當您嘗試訪問元素時,將引發異常。 初始化數組的元素,您應該沒問題。

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM