0

I am new to C#, and I want to make a Chess Game project in Visual Studio using said language. The main plan involves creating a virtual class named Piece that inherits the PictureBox class (I see the Piece class as simply an image with added functionalities) and it also has an abstract Move method that will be overridden in the inheriting classes (Pawn, Rook etc.). Each type of piece should set the image of the PictureBox based on what color that piece is, and I want this to happen during the call of the constructor for that class. How do I set the image of the PictureBox?

Most of the questions I've seen were similar but the PictureBox was not inherited, it was a nested class, or I would simply not understand a lot of the functions people were talking about which is why I asked a very specific question.

The constructor for my Pawn class currently looks like this:

    public Pawn(Coordinate coordinate, int color): base(coordinate, color)
    {
        if(color==BLACK)
        {
            //Get the BlackPawn.png image and use it
        }
        if(color==WHITE)
        {
            //Get the WhitePawn.png image and use it
        }
        //Other stuff
    }

Also, if I inherit the PictureBox class do I also need to call its constructor in the Piece class constructor and be mindful of its parameters, like I did with the Pawn constructor:

    Pawn(Coordinate coordinate, int color): base(coordinate, color){/*...*/}
  • If you inherit `PictureBox` your class *is* a `PictureBox`, so set the `Image` or `ImageLocation` property. Seems like a simple thing to try before posting. Be mindful that you may need to dispose of the images you make – Ňɏssa Pøngjǣrdenlarp Mar 04 '18 at 20:34
  • No. The pawn class constructor will automatically call the picturebox constructor before the pawn class constructor is called. – jdweng Mar 04 '18 at 20:58

1 Answers1

0

Inheriting means, that you somewhere declared

class Piece : PictureBox

which makes this condition true:

Pawn is PictureBox

We can also say, PictureBox is a base class of Pawn via Piece. To access any but private members of your base class, you just use them, qualification with the this keyword is optional.

this.Image = ...;
this.Visible = true;

The second part of your question, constructor chaining, is covered here.
PictureBox does not have any but the default constructor though, so the question of whether or not to pass constructor arguments is obsolete. You can rely on the framework to always call the default constructor, no need to write code for that.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77