Home > OS >  Inheritance Issue in C# : CS7036
Inheritance Issue in C# : CS7036

Time:07-02

For some reason I'm getting an error in the constructor method "ModifiedExercise" CS7036: There is no argument given that corresponds to the required formal parameter name of Exercise.Exercise(string,int,int,string,string[],Dictionary<string,bool>) I added the Parent and child classes Exercise and ModifiedExercise.

using System;
using System.Collections.Generic;
using System.Text;

namespace Trainer_App
{
    internal class ModifiedExercise : Exercise 
    {
        private Exercise regular_exercise;
        public ModifiedExercise(Exercise regular_exercise,
                                string name,
                                int min_num_balls,
                                int participants_num,
                                string court_type,
                                string[] accessories,
                                Dictionary<string, bool> exerciseTypes)
        {
            this.regular_exercise = regular_exercise;
            this.id = Exercise.id_counter;
            Exercise.id_counter  ;
            this.name = name;
            this.min_num_balls = min_num_balls;
            this.participants_num = participants_num;
            this.court_type = court_type;
            this.accessories = accessories;
            this.exerciseTypes = exerciseTypes;

        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace Trainer_App
{
    internal class Exercise
    {
        protected static int id_counter = 0;
        protected int id;
        protected string name;
        protected int min_num_balls;
        protected int participants_num;
        protected string court_type;
        protected string[] accessories;
        protected static int exerciseCounter = 0;
        protected Dictionary<string, bool> exerciseTypes = new Dictionary<string, bool>();

        public Exercise(string name,int min_num_balls, int participants_num,string court_type, string[]accessories,Dictionary<string,bool> exerciseTypes)
        {
            this.id = id_counter;
            id_counter  ;
            this.name = name;
            this.min_num_balls = min_num_balls;
            this.participants_num = participants_num;
            this.court_type = court_type;
            this.accessories = accessories;
            this.exerciseTypes = exerciseTypes;
        }



    }
}

CodePudding user response:

By default constructors effectively call : base() on their base type

You have two options here:

  1. Add a protected Exercise(){} parameterless constructor to the base type
  2. Add an explicit : base(...) explicitly specifying which value to pass in which position to the base constructor

Note : 2 is the usual approach, to reduce duplication. This also allows the fields to remain private.

  • Related