[Mono-list] Some C# problems

Kenneth Christiaansen kenneth@gnu.org
Tue, 6 Aug 2002 00:48:06 +0200


He everyone,

I am having some problems with the attached code. This is the first I play
with C# so it might include sillynesses. It compiles fine, but I get the
following failures while running the code:

** (process:31789): WARNING **: unhandled exception
System.NullReferenceException: "A null value was found where an object
instance was required"
in (unmanaged) (managed to native wrapper) System.Array:GetLength (int)
in <0x00004> (managed to native wrapper) System.Array:GetLength (int)
in <0x00079> .Board:.ctor (int)
in <0x0005c> .TicTacToe:Main ()

So if anyone can give me a hint on what I am doing wrong, or tell me this is
a bug, then please feel free doing so.

Cheers, Kenneth


using System;

class TicTacToe {

 class Board {
  enum Block {
   Circle,
   Cross,
   None,
  };

  private Block [,] board;

  public Board (int size)
  {
   board = new Block [size, size];

   for (int i = 0; i < board.GetLength (0); i++) {
    for (int j = 0; j < board.GetLength (1); j++) {
     board [i, j] = Block.None;
    }
   }
  }

  public void DrawBoard ()
  {
   for (int i = 0; i < board.GetLength (0); i++) {
    for (int j = 0; j < board.GetLength (1); j++) {
     switch (board [i, j])
     {
     case Block.None:
      Console.Write (".");
      break;
     case Block.Cross:
      Console.Write ("X");
      break;
     case Block.Circle:
      Console.Write ("O");
      break;
     }
    }
    Console.WriteLine ("");
   }
  }
 }

 static public void Main ()
 {
  Console.WriteLine ("Welcome to Tic Tac Toe");
  Board board = new Board (10);
  board.DrawBoard ();
 }
}