none
no suitable method found to override

    Question

  • using System;
        class Pair
        {
            public int a, b;
            public Pair(int a, int b)
            {
                this.a = a;
                this.b = b;
            }
            public virtual Pair alu(Pair A)
            {
                return new Pair(a - A.a, b - A.b);
            }
            public virtual Pair kosu(Pair A)
            {
                return new Pair(a + A.a, b + A.b);
            }
            public virtual void info()
            {
                Console.WriteLine("a=" + a + ",b=" + b);
            }
        }
        class Racional_san : Pair
        {
            public Racional_san(int a, int b) : base(a, b) { }
            public override Racional_san alu(Racional_san A)
            {
                return new Racional_san(a * A.b - b * A.a, b * A.b);
            }
            public override Racional_san kosu(Racional_san A)
            {
                return new Racional_san(a * A.b + b * A.a, b * A.b);
            }
            public override void info()
            {
                base.info();
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Pair A = new Pair(2, 4);
                Pair B = new Pair(1, 2);
                Pair C = A.alu(B);
                Pair D = A.kosu(B);
                Console.Write("ob1: ");
                A.info();
                Console.Write("ob2: ");
                B.info();
                Console.WriteLine("azaityndysy(ob1-ob2): " + C.a + "," + C.b);
                Console.WriteLine("kosyndysy(ob1+ob2): " + D.a + "," + D.b);
                Racional_san E = new Racional_san(2, 4);
                Racional_san F = new Racional_san(1, 2);
                Console.Write("ob3(racional): ");
                D.info();
                Console.Write("ob4(racional): ");
                E.info();
                Console.WriteLine("azaityndysy(ob3-ob4): " + F.a + "," + F.b);
                Racional_san G = E.alu(F);
                Racional_san H = E.kosu(F);
                Console.WriteLine("kosyndysy(ob3+ob4): " + H.a + "," + H.b);
                Console.ReadKey();
            }
        }

    //Error 1 'Racional_san.alu(Racional_san)': no suitable method found to override

    //Error 2 'Racional_san.kosu(Racional_san)': no suitable method found to override

    Thursday, December 6, 2018 5:08 PM

All replies

  • Hello, 

    Signature of the method in derived class is not the same as in the base class. 

    Use the same function definition.


    Sincerely, Highly skilled coding monkey.


    Thursday, December 6, 2018 5:32 PM
  • Hello,
    You can give a written example, otherwise I did not understand what you said.

    Thanks in advance

    Thursday, December 6, 2018 7:17 PM

  • //Error 1 'Racional_san.alu(Racional_san)': no suitable method found to override

    //Error 2 'Racional_san.kosu(Racional_san)': no suitable method found to override

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

    "'function' : 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

    Thursday, December 6, 2018 7:51 PM
  • To override any method you must have same signature. Even though derived class is a child of parent and derived class has access  to properties  of parent class but it's not possible to hold all properties of parent into derived. SO you must have same signature. 

    Considering your code ,you can decide the behavior of object at run time

    example you have same declaration in base and derived class,

     Pair alu(Pair A);

    Now at a time of calling the object at you can pass the derived object rather than base object,So function call code in main method will be like,

    //create base class object

    Pair objPair = new Pair();

    //Create derived class Object

    Racional_san objRecional_san = new Racional_san()

    // Pass derived object to method rather than parent object

    objPair = objPair.alu(objRecional_san);

    // pass parent object

    objPair = objPair.alu(objPair);

    //call derived with parent object

    objRecional_san.alu(objPair);

    //Call derived with derived object

    objRecional_san.alu(objRecional_san)

     
    • Edited by Pavan R Ingale Thursday, December 6, 2018 8:42 PM appropriate word
    • Proposed as answer by Pavan R Ingale Thursday, December 6, 2018 8:43 PM
    Thursday, December 6, 2018 8:40 PM
  • class Racional_san : Pair
        {
            public Racional_san(int a, int b) : base(a, b) { }
            public override Pair alu(Pair A)
            {
                return new Racional_san(a * A.b - b * A.a, b * A.b);
            }
            public override Pair kosu(Pair A)
            {
                return new Racional_san(a * A.b + b * A.a, b * A.b);
            }
            public override void info()
            {
                base.info();
            }
        }


    Sincerely, Highly skilled coding monkey.

    Friday, December 7, 2018 12:14 PM
  • Thank you, but I need to get the alu method Racional_san, ie the override in a derivative class, that is, the types of alu or kosu should be Racional_san
    Saturday, December 8, 2018 4:01 AM
  • Thank you, but I need to get the alu method Racional_san, ie the override in a derivative class, that is, the types of alu or kosu should be Racional_san

    Then it's just plain not an override.  The virtual/override keywords are for use when your base class contains a function definition that you want to extend in your derived class.  You aren't doing that, because you're writing a brand new function with the same name but different return value. 

    class Pair
        {
            public int a, b;
            public Pair(int a, int b)
            {
                this.a = a;
                this.b = b;
            }
    
            // People keep trying to tell you that the signatures don't match
            public virtual Pair alu(Pair A) // This is the signature of the base class's alu function - THIS LINE ONLY
            {
                return new Pair(a - A.a, b - A.b);
            }
            public virtual Pair kosu(Pair A)
            {
                return new Pair(a + A.a, b + A.b);
            }
            public virtual void info()
            {
                Console.WriteLine("a=" + a + ",b=" + b);
            }
        }
        class Racional_san : Pair
        {
            public Racional_san(int a, int b) : base(a, b) { }
    
            // An example of legitimate use of the override keyword
            public override Pair alu(Pair A) // Notice that the signature line is identical to the signature in the base class
            {
                Pair outputValue = base.alu(A); // To extend the funtionality of the original, start with its output value
                outputValue.a += 14; // Now meddle with it
                return outputValue; // That's an override
            }
    
            // What you need to do with your non-override method alu
            public Racional_san alu(Racional_san A) // Notice that the signature line is completely different.  Different return type, different parameter type.  Only the function name is the same.  You not only don't need an override, but there's no way this actually CAN override because it's just plain not similar
            {
                return new Racional_san(a * A.b - b * A.a, b * A.b);
            }
            public Racional_san kosu(Racional_san A)
            {
                return new Racional_san(a * A.b + b * A.a, b * A.b);
            }
            public override void info()
            {
                base.info();
            }
        }


    It never hurts to try. In the worst-case scenario, you'll learn something.

    Saturday, December 8, 2018 5:08 AM
  • Thank you
    Saturday, December 8, 2018 6:28 AM
  • Happy to help :-)  Best wishes!

    It never hurts to try. In the worst-case scenario, you'll learn something.

    Saturday, December 8, 2018 6:44 AM
  •  I need to get the alu method Racional_san, ie the override in a derivative class, that is, the types of alu or kosu should be Racional_san

    You may want to review the differences between "overriding" and "overloading".

    C# Method Overloading and Overriding With Examples
    https://www.dotnetforall.com/method-overloading-and-overriding-with-examples/

    Overloading and overriding
    https://stackoverflow.com/questions/673721/overloading-and-overriding

    Overloading
    http://www.csharpindepth.com/Articles/General/Overloading.aspx

    - Wayne

    Saturday, December 8, 2018 7:31 AM