none
override code RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }
    public class N
    {
        public int M()
        {
            return 20;
        }
        public class O
        {
            public virtual int M1(int x, int y)
            {
                return 10;
            }
        }
    }
    
    public class P
    { 
    //override the virtual method M present in class O
       // public override int M1(int x, int y)
       // {
        //    return 30;
    
       // }
    
    
    }
    my Aim is to override the virtual method M present in class O. I did but i am getting error. Can anybody tell me any other way to do it???
    • Moved by Jason Dot Wang Tuesday, January 29, 2013 5:52 AM This thread is about ASP.NET
    Sunday, January 27, 2013 1:07 PM

All replies

  • Class P does no inherit from class O so you will get an error that no suitable method could be found to override. This code will compile:

        public class N
        {
            public int M()
            {
                return 20;
            }
    
            public class O
            {
                public virtual int M1(int x, int y)
                {
                    return 10;
                }
            }
    
        }
    
        public class P : N.O
        {
            //override the virtual method M present in class O
            public override int M1(int x, int y)
            {
                return 30;
    
            }
    
        }

    • Proposed as answer by chriga Sunday, January 27, 2013 10:21 PM
    Sunday, January 27, 2013 1:15 PM
  • Reconsider this kind of design. Nested public classes make under most circumstances no sense.
    Sunday, January 27, 2013 1:20 PM
  • Hi

    Jchase

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            P p1 = new P();
            N.O O1 = new N.O();
            int r1=O1.M1(20,10);
            Response.Write(r1);
     
    
        }
    }
    public class N
    {
        public int M()
        {
            return 20;
        }
        public class O
        {
            public virtual int M1(int x, int y)
            {
                return x+y;
            }
        }
    }
    
    public class P:N.O
    { 
    //override the virtual method M present in class O
        public override int M1(int x, int y)
        {
            return 50;
    
        }
    
    
    }
    I am calling Virtual method present in class O. it is printing 30 but method is override in class p so output should be 50. please see code

    Sunday, January 27, 2013 1:34 PM
  • Your O1 is an N not a P therefore will call the M1 in N.O not the one in P.O.

    I'd think about what Stefan says - this looks iffy.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Sunday, January 27, 2013 2:25 PM
  • As your code really has nothing to do with ASP.Net, I changed it to a console app.  See the following, which shows the difference in p1.M1(20, 10) and O1.M1(20, 10).  They are different object types.
     
     public class N
     {
      public int M()
      {
       return 20;
      }
      public class O
      {
       public virtual int M1(int x, int y)
       {
        return x + y;
       }
      }
     }
     
     public class P : N.O
     {
      //override the virtual method M present in class O
      public override int M1(int x, int y)
      {
       return 50;
     
      }
     

     }
     class Program
     {
      static void Main(string[] args)
      {
       P p1 = new P();
       N.O O1 = new N.O();
       int r1 = O1.M1(20, 10);
       int r2 = p1.M1(20, 10);
     
       Console.WriteLine(r1);
       Console.WriteLine(r2);
     
       Console.WriteLine("done");
       Console.ReadKey();
      }
     }

    --
    Mike
    Sunday, January 27, 2013 5:13 PM
  • Try changing:

      protected void Page_Load(object sender, EventArgs e)
      {
       P p1 = new P();
       N.O O1 = new N.O();
       int r1 = O1.M1(20, 10);
       Response.Write(r1);
      }
    

    to:

      protected void Page_Load(object sender, EventArgs e)
      {
       P p1 = new P();
       //N.O O1 = new N.O();
       //int r1 = O1.M1(20, 10);
       int r1 = p1.M1(20, 10);
       Response.Write(r1);
      }
    


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Sunday, January 27, 2013 9:53 PM
  • Hi love4csharp,

      Welcome to MSDN Forum Support.

     

      You'll need to post it in the dedicated ASP.Net Forum http://forums.asp.net for more efficient responses, where you can contact ASP.NET experts.

      Sincerely,

      Jason Wang



    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, January 29, 2013 5:52 AM
  • actually I am using asp.net for writing c# code. 

    see above code

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }

    this is acting as a main method. My concept is totally based on C#.

    • Edited by love4csharp Tuesday, January 29, 2013 6:48 AM
    Tuesday, January 29, 2013 5:59 AM