0

when i compile the code below the compiler give the error:A field initializer cannot reference the non-static field, method, or property in this code(which have stars)

 KingPiece kingPiece = new KingPiece(***siyahsah1***,ChessColor.White);

can anyone help me?

 class PiecePosition
{

    public enum ChessColor
    {
        White,
        Black,
    }
    public class ChessPiece
    {

        private Image DisplayedImage;
        private ChessColor DisplayedColor;
        private Point CurrentSquare;
        public Point[] ValidMoves;
        public ChessPiece(Image image, ChessColor color)
        {
            DisplayedImage = image;
            DisplayedColor = color;
        }
    }

    public  class KingPiece : ChessPiece
    {



        public KingPiece(Image image, ChessColor color)
            : base(image, color)
        {


            ValidMoves[0] = new Point(0, -1);    //  Up 1
            ValidMoves[1] = new Point(1, -1);  //  Up 1, Right 1
            ValidMoves[2] = new Point(1, 0);     //  Right 1

            ValidMoves[7] = new Point(-1, -1);  //  Left 1, Up 1
        }

        System.Drawing.Bitmap siyahsah1 = chess6.Properties.Resources.siyahsah1;
        KingPiece kingPiece = new KingPiece(siyahsah1,ChessColor.White);


    }

}
Arash
  • 3,013
  • 10
  • 52
  • 74
  • Please do not post unrelevant code, it makes reading difficult, the clas Board in your example is irrelevant to your question. Besides making Board class as nested to PiecePosition is a bit strange – Captain Comic Nov 06 '10 at 16:49

3 Answers3

4

You should move the line into the constructor:

kingPiece = new KingPiece(siyahsah1,ChessColor.White);

The problem is that you don't yet have a class instance and the compiler doesn't like that. If you simply move the line into the constructor, it'll work.

You'll still have to define the property as a private field. You can do this with:

private KingPiece kingPiece;

Do this at the current location of the first line. The end result will be exactly like what you now heave, except that it will compile.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
2

The error is self explaining. siyahsah1 is a non-static private property of KingPiece and not is not initialized at the moment constructor is called.

Captain Comic
  • 15,744
  • 43
  • 110
  • 148
1

As others have said, siyahsah1 is a non-static private field, which cannot be used to initialize other fields. But you have another problem here. Because the KingPiece class has only one constructor, you cannot create a new instance of that class in the constructor itself - it will produce a StackOverflowException. Workaround would be to create another, private constructor which could only be called from within the KingPiece class. But better yet, maybe you could tell us exactly what you want to do and we can help you with that.

Update: Considering the comments I'm guessing that arash wants to specify the image to be used with the KingPiece class. If only one image is used independent of the piece's color, then you can just pass that argument to the base constructor.

public class KingPiece: ChessPiece {
  public KingPiece(ChessColor color):
    // Pass the image to the base class. All king pieces will use this image.
    base(chess6.Properties.Resources.siyahsah1, color) {
    ..
  }
}

But if you want to have different images for each color then that should be defined elsewhere or maybe as a static field/property.

public class KingPiece: ChessPiece {
  public static readonly BlackKing = new KingPiece(/* image of the black king here */, ChessColor.Black);
  public static readonly WhiteKing = new KingPiece(/* image of the white king here */, ChessColor.White);

  // Constructor could also be made private since you probably don't need other instances beside black and white.
  public KingPiece(Image image, ChessColor color): base(image, color) {
    ...
  }
}

Hope this helps.

Patko
  • 4,365
  • 1
  • 32
  • 27