none
Method Overriding Error in C# RRS feed

  • Question

  • Hi There.

    i am learning C# programing. when i was practicing method overriding concept i got below error.

    please any body can help me to address this issue i would be grateful.

    My Code:

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


    namespace PolyMorphism
    {
    public class PolyMorphism1
    {
    public int getPolyTotalCost(int intQty)
    {
    return intQty;
    }
    public virtual int getPolyTotalCost(int intQty , int intProduct)
    {
    return intQty * intProduct;
    }



    }
    public class PolyMorphism2 : PolyMorphism1
    {
    public override int getPolyTotalCost(int intQty, int intProduct, int intPerProDiscount) // I am getting error this line...." No suitable method found to override "
    {
    return (getPolyTotalCost(intQty, intProduct) - intPerProDiscount);
    }
    }


    }


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using PolyMorphism;


    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    PolyMorphism1 Poly1 = new PolyMorphism1(); 
    PolyMorphism2 Poly2 = new PolyMorphism2();
    Console.WriteLine("Enter Product Price:"); 
    int a = Convert.ToInt16(Console.ReadLine());
    Console.WriteLine("Enter Product Quantity:");
    int b = Convert.ToInt16(Console.ReadLine());
    Console.WriteLine("Total is:" + Poly1.getPolyTotalCost(a, b));
    Console.WriteLine("Enter Product Discount:");
    int c = Convert.ToInt16(Console.ReadLine());
    Console.WriteLine("The total after discount is");
    Console.WriteLine(Poly2.getPolyTotalCost(a, b, c)); 
    Console.ReadLine();
    }
    }
    }
    Thursday, November 22, 2018 4:19 AM

All replies



  • namespace PolyMorphism
    {
    public class PolyMorphism1
    {
    public int getPolyTotalCost(int intQty)
    {
    return intQty;
    }
    public virtual int getPolyTotalCost(int intQty , int intProduct)
    {
    return intQty * intProduct;
    }



    }
    public class PolyMorphism2 : PolyMorphism1
    {
    public override int getPolyTotalCost(int intQty, int intProduct, int intPerProDiscount) // I am getting error this line...." No suitable method found to override "


    override (C# Reference)
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/override

    "An override method provides a new implementation of a member that is 
    inherited from a base class. The method that is overridden by an override 
    declaration is known as the overridden base method. The overridden base 
    method must have the same signature as the override method."

    - Wayne

    • Proposed as answer by Tim Roberts Tuesday, November 27, 2018 6:25 PM
    Thursday, November 22, 2018 5:11 AM

  • public class PolyMorphism2 : PolyMorphism1
    {
    public override int getPolyTotalCost(int intQty, int intProduct, int intPerProDiscount) // I am getting error this line...." No suitable method found to override "
    {
    return (getPolyTotalCost(intQty, intProduct) - intPerProDiscount);
    }
    }

    Compiler Error CS0115
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0115

    "'function' : no suitable method found to override"

    - Wayne

    Thursday, November 22, 2018 5:24 AM
  • By another words when you want to use virtual/override then you change behaviour of method with same parameters as base method. There could be some differences when you use override/new keyword.

    When you wan to extend method with parameter you cannot use override keyword.

    Thursday, November 22, 2018 6:05 AM
  • hi wayne.

    thank you very much for your kind explanation.

    Thursday, November 22, 2018 10:31 AM
  • Just some comments as you're currently starting to learn C#:

    - The common casing in C# is a mix of camelCase and PascalCase. Class and method names, properties and public fields are PascalCase. Private fields, parameters and local variables are camelCase.

    - Get and Set a special verbs. They should be only used for explicit getter and setter methods.

    - Use verbose naming. Abbreviations should only be used, when they are part of the lingo and everybody knows them.

    - imho the majority does not use Hungarian prefixes for data types.

    e.g.

    public class PolyMorphism1
    {
        public int CalculateCost(int quantity)
        {
            return quantity;
        }
    
        public virtual int CalculateCost(int quantity, int price)
        {
            return quantity * price;
        }
    }
    
    public class PolyMorphism2 : PolyMorphism1
    {
        public int CalculateCost(int quantity, int price, int overallDiscount)
        {
            return this.CalculateCost(quantity, price) - overallDiscount;
        }
    }

    But as your goal seems to be polymorphism, then you need to think about types here:

    namespace ConsoleCS
    {
        using System;
    
        public class OrderItem
        {
            public int Price { get; set; }
            public int Quantity { get; set; }
            public virtual int Total()
            {
                return this.Price * this.Quantity;
            }
            public override string ToString()
            {
                return $"Total: {this.Total()}";
            }
        }
    
        public class DiscountedOrderItem : OrderItem
        {
            public int Discount { get; set; }
            public override int Total()
            {
                return base.Total() - this.Discount;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                OrderItem orderItem1 = new OrderItem() { Price = 7, Quantity = 3 };
                OrderItem orderItem2 = new DiscountedOrderItem() { Price = 7, Quantity = 3, Discount = 3 };
                Console.WriteLine($"#1: {orderItem1}");
                Console.WriteLine($"#1: {orderItem2}");
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
    
        }
    }

    or using a base class

    namespace ConsoleCS
    {
        using System;
    
        public abstract class BaseOrderItem
        {
            public abstract int Total();
            public override string ToString()
            {
                return $"Total: {this.Total()}";
            }
        }
    
        public class OrderItem : BaseOrderItem
        {
            public int Price { get; set; }
            public int Quantity { get; set; }
            public override int Total()
            {
                return this.Price * this.Quantity;
            }
        }
    
        public class DiscountedOrderItem : OrderItem
        {
            public int Discount { get; set; }
            public override int Total()
            {
                return base.Total() - this.Discount;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                BaseOrderItem orderItem1 = new OrderItem() { Price = 7, Quantity = 3 };
                BaseOrderItem orderItem2 = new DiscountedOrderItem() { Price = 7, Quantity = 3, Discount = 3 };
                Console.WriteLine($"#1: {orderItem1}");
                Console.WriteLine($"#1: {orderItem2}");
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
    
        }
    }

    Thursday, November 22, 2018 11:29 AM
  • You need the SAME number of parameters in your overriding method as in the method you will override.

    As Wayne says:

    the overridden base 
    method must have the same signature as the override method."

    • Edited by ThisNewbie Tuesday, November 27, 2018 11:16 AM
    Tuesday, November 27, 2018 11:15 AM
  • When you cite another post, then you should do a correct recap.

    The same number of parameters is not sufficient. Cause the parameter signature must be the same. The signature includes the data type as well as the number of the parameters.

    Tuesday, November 27, 2018 12:23 PM
  • @Stefan

    When you cite another post, then you should do a correct recap.

    The same number of parameters is not sufficient. Cause the parameter signature must be the same. The signature includes the data type as well as the number of the parameters.

    @Stefan, mr. ultraLowQualityPoints:

    >> The signature includes the data type as well as the number of the parameters

    ... and the method's name!

    It's not possible to override any method without specifying (1) the same method name, (2) the same parameter type(s) and the same returned type.

    Note #1: the returned type is not part of the signature.

    Note #2: indirectly, this is why you had to return object when implementing ICloneable, in another (recent) thread.



    • Edited by ritehere44 Thursday, November 29, 2018 6:43 AM missing img
    Wednesday, November 28, 2018 7:24 AM