views:

751

answers:

5

I would suspect the code below to output:

(I am a SmartForm object and using the method in SmartForm).xml

instead, however, it outputs:

(I am a SmartForm object and using the method in Item).xml

Why is that? How can I force C# to take the value from the overriding property? That is why I am overriding the property.

using System;

namespace TestInhersdk234
{
    public class Program
    {
        static void Main(string[] args)
        {
            SmartForm smartForm = new SmartForm();
            Console.ReadLine();
        }
    }

    public class SmartForm : Item
    {
        public SmartForm()
        {
            Console.WriteLine(FullXmlDataStorePathAndFileName);
        }

        public new string GetItemTypeIdCode
        {
            get
            {
                return String.Format("(I am a {0} object and using the method in SmartForm)", this.GetType().Name);
            }
        }
    }

    public class Item
    {
        public string FullXmlDataStorePathAndFileName
        {
            get
            {
                return GetItemTypeIdCode + ".xml";
            }
        }

        public string GetItemTypeIdCode
        {
            get
            {
                return String.Format("(I am a {0} object and using the method in Item)", this.GetType().Name);
            }
        }
    }
}
+6  A: 

You need to add the override keyword to the overriding method?

second
thanks, different than PHP, :-)
Edward Tanguay
This actually isn't 100% of your answer ... But it's part of it. :)
John Rudy
+13  A: 

You're not actually overriding. You're hiding. To override:

class MyBase
{
    public virtual void foo() {}
}

class MyClass : MyBase
{
    public override void foo() {}
}
Jon B
If in the above code SmartForm.GetItemTypeIdCodeis hiding the property Item.GetItemTypeIdCode, it is odd that the property that gets executed is called "hidden". I would think the "hidden" property would not be executed. How does one reconcile this terminology?
Edward Tanguay
You're hiding the original functionality in the new class, and replacing it with something else. However, in your case you're calling the original function from the base class. You definitely want to override in this case.
Jon B
+3  A: 

Item's properties which you wish to override are not marked virtual. As a result, when you redefine them in SmartForm, you are merely "hiding" them, and not actually overriding them. (Additionally, you will need the override keyword in SmartForm as well.)

Check out this guide.

John Rudy
A: 

GetItemTypeIdCode isn't virtual; you're not overriding it, you're just hiding it. In that case, which method gets executed isn't based on the dynamic type of the object, but on the static type of the object reference. Inside FullXmlDataStorePathAndFileName, the static type of 'this' is Item, not SmartForm, so the Item implementation of GetItemTypeIdCode is called.

Skirwan
A: 

the property in the base class should be marked virtual

also the property in the inherited class should be marked overrides

also i am not sure why the keyword new appears in your code in the property definition

John Nicholas
new on a property or method in a derived class is a way of indicating to the C# compiler that you actually intend to hide the parent method/prop, instead of overriding it. IIRC, it squashes the compiler warning regarding doing so. However, it's been so long since I've actually NEEDED to hide (vs override) that I could be a little rusty there. :)
John Rudy