Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am having a problem with my code. I created a parent class and overridden it with a child class but it says there is not suitable method to override and I do not understand why this is happening. I looked on the internet but to no avail.

My code for the parent class is:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wood_Dylan
{
   abstract class StudentClass
    {
        //Create private backing variables for properties 

        
        //creating properties
        public decimal TotalTuition { get; set; }
        public int Hours { get; set; }
        public decimal BaseRate { get; set; }
        public decimal GraduateFee { get; set; }

        //Method to calculate graduate tuition
        public virtual decimal CalcTuition(int intCreditHours, decimal decBaseRate, decimal decGraduateFee, int intExcessCredits, decimal decExcessCreditsPercentage)
        {
            //declare variables
            Hours = intCreditHours;
            BaseRate = decBaseRate;
            GraduateFee = decGraduateFee;

            TotalTuition = (Hours * BaseRate) + GraduateFee;

            return TotalTuition; 
        }
        


    }
}


The code for the child class is:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Wood_Dylan
{
    class UndergraduateClass : StudentClass
    {

        //Create properties that are not in parent class
        public int ExcessCredits { get; set; }
        public decimal ExcessCreditsPercentage { get; set; }
        public decimal RegularTuition { get; set; }
        public decimal ExcessCreditTuition { get; set; }

       
        //Create method to calculate undergraduate tuition
        
        public override decimal CalcTuition(int intExcessCredits, decimal decExcessCreditsPercentage)
        {

           //populate properties not in Parent (student) class
            ExcessCredits = intExcessCredits;
            ExcessCreditsPercentage = decExcessCreditsPercentage;

            //calculate regular tuition
            RegularTuition = Hours * BaseRate;

            //Calc excess credit tuition
            ExcessCreditTuition = ExcessCredits * ExcessCreditsPercentage;

            //Calulate Total Tuition
            TotalTuition = RegularTuition + ExcessCreditTuition;

            return TotalTuition; 

        }

    }
}
Posted
Updated 2-Oct-15 7:21am
v2

The parameters in the derived class are different from the method in the abstract class.

Since the definition in the base class is
C#
public virtual decimal CalcTuition(int intCreditHours, decimal decBaseRate, decimal decGraduateFee, int intExcessCredits, decimal decExcessCreditsPercentage)
{

Instead of the following
C#
public override decimal CalcTuition(int intExcessCredits, decimal decExcessCreditsPercentage)
{

try
C#
public override decimal CalcTuition(int intCreditHours, decimal decBaseRate, decimal decGraduateFee, int intExcessCredits, decimal decExcessCreditsPercentage) 
{
 
Share this answer
 
To override a method you must have method signature same.
In your base class you have method "CalcTuition" having 5 input parameters.
public virtual decimal CalcTuition(int intCreditHours, decimal decBaseRate, decimal decGraduateFee, int intExcessCredits, decimal decExcessCreditsPercentage)

While in your child class you have method "CalcTuition" having 2 input parameters.
public override decimal CalcTuition(int intExcessCredits, decimal decExcessCreditsPercentage)


To override you should have method signature same ie. return type and number/type of parameter will be same.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900