Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3332356
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:40:47+00:00 2026-05-17T23:40:47+00:00

when i compile the code below the compiler give the error:A field initializer cannot

  • 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);


    }

}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:40:47+00:00Added an answer on May 17, 2026 at 11:40 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation below. This code will throw a compiler error for
Why does the code block below give a compile error of does not contain
How can this code compile? The code below in the operator int CAN access
I’m getting system error when I try to compile the code below on Visual
I have presented the code below. The compiler throws error when I overload an
It is a compiler error or runtime error? The code below can be compiled!
The code bellow will give compile error on line enum en = A::en; but
I can't get the code below to compile (see errors). Advice on correction would
My code below won't compile. What am i doing wrong? I'm basically trying to
When compiling the code below with cl /c /clr /W4 the compiler says warning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.