none
How to override a Protected Property? RRS feed

  • Question

  • hello msdn, 

    How can I override a property of base class having protected access modifer?

    Suppose I have some protected properties in a class, How can I override those Properties, 

    For example: 

    public class BaseClass
    {
    protected Class1 Class1Property;
    protected internal class2 Class2Property;
    }
    
    public class DerivedClass: BaseClass
    {
    //How can I override Protected properties of base class?
    }
    

    Regards

    Salam


    salampv.07

    Wednesday, January 16, 2013 11:18 AM

Answers

  • Hi,
    first of all that property should be virtual in base class(or it should be overridden there) if you want to override it in  derived class.

    So, your code goes like this:

     public class class2 { }
    
        public class BaseClass
        {
            protected class2 Class1Property;
            protected virtual internal class2 Class2Property
            {
                get
                {
                    if (Class1Property == null)
                    {
                        //Class1Property = SomeMethod();
                    }
                    return Class1Property;
                }
            }
        }
    
        public class DerivedClass : BaseClass
        {
            protected internal override class2 Class2Property
            {
                get
                {
                   // define your logic.
                }
            }
            
        }

    Hope this helps.


    One good question is equivalent to ten best answers.

    Wednesday, January 16, 2013 1:00 PM

All replies

  • Your code is showing fields, not properties.  You cannot override fields, though you can change their values.

    What exactly do you want exposed in your DerivedClass?


    This signature unintentionally left blank.

    Wednesday, January 16, 2013 11:59 AM
  • Hello Nick F,

           Thank you very much for the clarification.And I am very sorry for the above code. I modify it to

    public class BaseClass { protected class2 Class1Property; protected internal class2 Class2Property { get{

    if(Class1Property == null) { Class1Property = SomeMethod(); } return Class1Property;

    } } } public class DerivedClass: BaseClass { //How can I override Protected properties of base class? }


    Regards

    Salam



    salampv.07


    • Edited by salam.p.v Wednesday, January 16, 2013 12:15 PM
    Wednesday, January 16, 2013 12:14 PM
  • Hi,
    first of all that property should be virtual in base class(or it should be overridden there) if you want to override it in  derived class.

    So, your code goes like this:

     public class class2 { }
    
        public class BaseClass
        {
            protected class2 Class1Property;
            protected virtual internal class2 Class2Property
            {
                get
                {
                    if (Class1Property == null)
                    {
                        //Class1Property = SomeMethod();
                    }
                    return Class1Property;
                }
            }
        }
    
        public class DerivedClass : BaseClass
        {
            protected internal override class2 Class2Property
            {
                get
                {
                   // define your logic.
                }
            }
            
        }

    Hope this helps.


    One good question is equivalent to ten best answers.

    Wednesday, January 16, 2013 1:00 PM
  • For more information about virtual and override keywords in C# please visit here.

    One good question is equivalent to ten best answers.

    Wednesday, January 16, 2013 1:03 PM