I've used Singletons in Java, but I just can't seem to get it right with C#. I get a TypeInitializationException on the Checkers.Game class. Here's my code...
using System;
using System.Collections.Generic;
using System.Text;
namespace Checkers
{
class Program
{
public static void Main()
{
Game _Game = Game.Instance;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Checkers
{
public sealed class Game
{
private static Game _Instance = new Game();
private Board _Board = Board.Instance;
private Player[] _Players = new Player[2];
public static Game Instance
{
get
{
return _Instance;
}
}
public Board Board
{
get
{
return _Board;
}
set
{
_Board = value;
}
}
public Player[] Players
{
get
{
return _Players;
}
set
{
_Players = value;
}
}
private Game()
{
Players[0] = new Player(Player.Directions.Down);
Players[1] = new Player(Player.Directions.Up);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Checkers
{
public sealed class Board
{
private static Game _Game = Game.Instance;
private static Board _Instance = new Board();
private Piece[,] _Pieces = new Piece[8, 8];
public static Board Instance
{
get
{
return _Instance;
}
}
public Piece[,] Pieces
{
get
{
return _Pieces;
}
set
{
_Pieces = value;
}
}
private Board()
{
int X;
int Y;
for (X = 0; X < 8; X++)
{
for (Y = 0; Y < 2; Y++)
{
Pieces[X, Y] = new Piece(X, Y, _Game.Players[0]);
}
}
}
}
}
The exception I get is...
System.TypeInitializationException was unhandled
Message="The type initializer for 'Checkers.Game' threw an exception."
Source="Checkers"
TypeName="Checkers.Game"
StackTrace:
at Checkers.Game.get_Instance()
at Checkers.Program.Main() in C:\Documents and Settings\cbasnett\My Documents\Visual Studio 2005\Projects\Checkers\Checkers\Program.cs:line 11
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Loading
AlanPosted Feb 22, 2008, 5:44 AM
After adding some 'dummy' declarations for Player, Piece etc, the code compiled and ran fine when I tried it just now and so the problem may not necessarily lie with your singletons but elsewhere.
However, you could try adjusting the singleton declarations as follows, so that they are not instantiated when the static _Instance fields themselves are initialized by the system but instantiation is deferred until the Instance properties are accessed:
using System;
using System.Collections.Generic;
using System.Text;
namespace Checkers
{
public sealed class Game
{
private static Game _Instance = null;
private Board _Board = Board.Instance;
private Player[] _Players = new Player[2];
public static Game Instance
{
get
{
if (_Instance == null) _Instance = new Game();
return _Instance;
}
}
public Board Board
{
get
{
return _Board;
}
set
{
_Board = value;
}
}
public Player[] Players
{
get
{
return _Players;
}
set
{
_Players = value;
}
}
private Game()
{
Players[0] = new Player(Player.Directions.Down);
Players[1] = new Player(Player.Directions.Up);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Checkers
{
public sealed class Board
{
private static Game _Game = Game.Instance;
private static Board _Instance = null;
private Piece[,] _Pieces = new Piece[8, 8];
public static Board Instance
{
get
{
if (_Instance == null) _Instance = new Board();
return _Instance;
}
}
public Piece[,] Pieces
{
get
{
return _Pieces;
}
set
{
_Pieces = value;
}
}
private Board()
{
int X;
int Y;
for (X = 0; X < 8; X++)
{
for (Y = 0; Y < 2; Y++)
{
Pieces[X, Y] = new Piece(X, Y, _Game.Players[0]);
}
}
}
}
}