Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am I getting a C7036 error in my constructor hierarchy?

A group and I are working on a small game in C# with some help from our professor, but we’re getting a strange error when trying to create a construction hierarchy that we can’t seem to find the cause for. Both the relevant parts of the Player class (where the error is) and the Character class that Player inherits from are below.

Player class:

public class Player : Character 
    {
        public int LVL {get; private set;}  // Player level
        public int XP {get; private set;}  // Experience points
        public int MXP {get; private set;} // Max Exp
        public int COIN {get; private set;} // Player Coins 
        private Room _currentRoom = null;
        public Room CurrentRoom { get { return _currentRoom; } set { _currentRoom = value; } }

        public Player() : this(null) { }
        public Player(Room room) : this(room, null) { }
        public Player(Room room, string? name) : this(room, name, 0) { }
        public Player(Room room, string? name, int hp) : this(room, name, hp, 0) { }
        public Player(Room room, string? name, int hp, int atk) : this(room, name, hp, atk, 0){}
        public Player(Room room, string name, int hp, int atk, int def)
               ^(C7036 is thrown here. There is no argument given that corresponds to the required parameter 'name' of 'Character.Character(string, int, int, int)'CS7036)
        {
            _currentRoom = room;
            Name = name;
            HP = hp;
            ATK = atk;
            DEF = def;
            LVL = 1; // to start
            XP = 0;
            MXP = 13;
            COIN = 13;
        }

Character class:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  public class Character : ICharacter{
        private string? _name;
        public string? Name{
            get {
               return _name; 
            }
            set{ 
                _name = value; 
            }
        }
    
        private int _hp;
        public int HP{
            get{
                return _hp;
            }
            set{
                _hp = value; 
            }
        }
        private int _atk; 
        public int ATK{
            get{
                return _atk;
            }
            set{
                _atk = value; 
            }
        }
        private int _def; 
        public int DEF{
            get{
                return _def;
            }
            set{
                _def = value; 
            }
        }

        public Character(string name, int hp, int atk, int def){
            Name = name; 
            HP = hp; 
            ATK = atk; 
            DEF = def;  
        }

We’ve tried adjusting the hierarchy, making name nullable, as well as playing around with both the Player and Character constructors to see if it’ll recognize "name". So far nothing has been able to get rid of the error.

>Solution :

The constructors in Player cascade to each other, but don’t cascade up to its base class. Since Character doesn’t have a parameterless constructor, creating an instance of it requires providing the parameters it expects.

Similar to how the Player constructors cascade by calling this(), ultimately any construction also has to call base() with the right parameters. For example:

public Player(Room room, string name, int hp, int atk, int def)
    : base(name, hp, atk, def)
{
    _currentRoom = room;
    LVL = 1; // to start
    XP = 0;
    MXP = 13;
    COIN = 13;
}

Note that those properties then don’t need to be set within this constructor because the base constructor already does that.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading