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
    Saturday, December 8, 2018 6:28 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