I've been creating a Sudoku Solver in C# using Wpf as View. In the Constructor of the MainViewModel I have this code.
public MainViewModel()
{
SudokuTable = new char[9][];
for (int i = 0; i < 9; i++)
{
SudokuTable[i] = new char[9]
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
}
Solve = new RelayCommand(o => SudokuStart());
Reset = new RelayCommand(o => SudokuReset());
}
The RelayCommands are linked to Commands of 2 Buttons inside my View. The SudokuTable is bound to an Grid of Textboxes. Each Textbox is linked to a different jagged array Element. Example:
<TextBox Grid.Row = "0" Grid.Column = "0" Style = "{StaticResource GridTextBox}" Text = "{Binding Path=SudokuTable[0][0]}" />
As people might want to solve multiple Sudokus, I implemented a "Reset" Button. Once it's clicked it does this:
private void SudokuReset()
{
SudokuTable = new char[9][];
for (int i = 0; i < 9; i++)
{
SudokuTable[i] = new char[9]
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
}
}
So basically it does everything it would when starting to Application.
But for some reason all textboxes are empty as soon as I click on the Reset-Button. They don't contain ' ' which causes a NullReferenceException. Can anyone tell me the difference between the first Initialization and the reset of it? What am I doing wrong?
I'm thankful for all answers.
Edit as someone asked about my Property:
private char[][] sudokuTable;
public char[][] SudokuTable
{
get { return sudokuTable; }
set
{
if (sudokuTable != value)
{
sudokuTable = value;
OnPropertyChanged("SudokuTable");
}
}
}