Results 1 to 26 of 26

Thread: [RESOLVED] Method overridiing Issue

Hybrid View

  1. #1
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Method overridiing Issue

    There's a bit of misinformation being provided here. The C# 'new' keyword IS valid on methods and property declarations. The VB equivalent is Shadows. Shadowing a member and overriding it are similar but not the same. Generally speaking, it's preferable to declare a base member 'overridable' and then declare a derived member of the same name 'override'. If the base member isn't declared 'overridable' then you must declare the derived member 'new'. If you were to try Pradeep's code you would get an error message telling you that Driver can't be overridden.

    The main difference between overriding and shadowing is that overriding follows the type of the object, while shadowing follows the type of the reference. To demonstrate this, try running this code:
    Code:
    using System;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                BaseClass b = new BaseClass();
    
                Console.WriteLine("BaseClass object via BaseClass reference:");
                b.OverridableMethod();
                b.NotOverridableMethod();
                Console.WriteLine();
    
                DerivedClass d = new DerivedClass();
    
                Console.WriteLine("DerivedClass object via DerivedClass reference:");
                d.OverridableMethod();
                d.NotOverridableMethod();
                Console.WriteLine();
    
                b = d;
    
                Console.WriteLine("DerivedClass object via BaseClass reference:");
                b.OverridableMethod();
                b.NotOverridableMethod();
    
                Console.ReadLine();
            }
        }
    
        public class BaseClass
        {
            public virtual void OverridableMethod()
            {
                Console.WriteLine("BaseClass.OverridableMethod");
            }
    
            public void NotOverridableMethod()
            {
                Console.WriteLine("BaseClass.NotOverridableMethod");
            }
        }
    
        public class DerivedClass : BaseClass
        {
            public override void OverridableMethod()
            {
                Console.WriteLine("DerivedClass.OverridableMethod");
            }
    
            public new void NotOverridableMethod()
            {
                Console.WriteLine("DerivedClass.NotOverridableMethod");
            }
        }
    }
    As you'll see form the output, the first two cases are exactly as you'd expect: a BaseClass object via a BaseClass reference reports BaseClass methods and a DerivedClass object via a DerivedClass reference reports DerivedClass methods. It's the third case that gets interesting. When accessing a DerivedClass object via a BaseClass reference, the overridden method reports a DerivedClass method, while the shadowed method reports a BaseClass method. So the overridden members will always be invoked on the type of the object itself, while shadowed members will be invoked on the type of the reference, even if the actual object is a more derived type. This is why overriding is preferable, but shadowing is all you can do if you're not the author of the base class and they didn't declare the member virtual.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Method overridiing Issue

    Quote Originally Posted by jmcilhinney View Post
    There's a bit of misinformation being provided here. The C# 'new' keyword IS valid on methods and property declarations. The VB equivalent is Shadows. ...
    Great!
    So I learnt a new use of new keyword today.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width