Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override a property defined in base class

I have case where the class hierarchy is something like this,

   +---------------+
   | UIElement     |
   |---------------|                            +----------------------+
   | ...           |                            | My Windows Application
   | SomePropert{} |                            |----------------------|
   |               |<---+                       |+--------------------+|
   |               |    |                       ||MyUserControl       ||
   +---------------+    |                       ||--------------------||
         +--------------+-----+                 ||                    ||
         |FrameWorkElement    |                 |+--------------------+|
         |--------------------|                 |//Want to use         |
         |    ...             |<-+              |// SomeProperty;      |
         +--------------------+  |              |                      |
                     +-----------+-+            |                      |
                     |Control      |            |                      |
                     |-------------|            +----------------------+
                     |  ...        |<---+
                     +-------------+    |
                            +-----------+---+
                            | UserControl   |
                            |---------------|<---+
                            |  ...          |    |
                            +---------------+    |
                                      +----------+-------+
                                      | MyUserControl    |
                                      |------------------|
                                      | SomeProperty{}   |
                                      | //Want to override
                                      |                  |
                                      +------------------+

Now in my app (and all other apps where I can export this MyUserControl) can I set the SomeProperty that is handled by the MyUserControl class rather than UIElement?

I am right now doing this by creating an object to MyUserControl and assigning that to the control that I added in my xaml page.So right now looks like this,

MyUserControl newControl = new MyUserControl();
web = windowsPhoneControl11;  //windowsPhoneControll1 is the 
                              //one that I added from toolbox.
                              // i.e., mycustomecontrol added from toolbox.

So now since I am using the 'new' it gets overriden. But when I export this control I can't expect the user to create a new object and assign it to the control that one is using in the xaml page.

So is there any other way I could override this one property so that the assignment of that property is handled by MyUserControl class rather than the UIElement class? What I mean about MyUserControl having the control to set this property is that I need to check for some value before assigning it. If it is not atleast an expected value then I need to set it to a default value.

Ps: I am sorry for such a long question but I couldn't express it more precise and I was not able to find anyother question related to this. And it is WindowsPhoneApp... Not the ordinary windowsapplication.

like image 888
Ajai Avatar asked Feb 20 '12 20:02

Ajai


People also ask

How do you override a base class property?

you can override properties just like methods. That is, if the base method is virtual or abstract. You should use "new" instead of "override". "override" is used for "virtual" properties.

How do you override a property?

An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property. Beginning with C# 9.0, read-only overriding properties support covariant return types. The overridden property must be virtual , abstract , or override .

Can you override a property in C#?

In C#, class methods, indexers, properties and events can all be overridden. Non-virtual or static methods cannot be overridden. The overridden base method must be virtual, abstract, or override.

Which method of the base class derived class Cannot override?

Explanation: When an instance method declaration includes the sealed modifier, the method is said to be sealed method. It means a derived class cannot override this method.


1 Answers

Forgive me if I've interpreted this incorrectly but would the following work:

public class BaseClass
{
    public int MyProperty
    {
       get; set;
    }
}
public class ChildClass : BaseClass
{
    public new int MyProperty
    {
       get
       {
           return base.MyProperty;
       }
       set
       {
           if(DoYourCheckingStuff(value))
           {
               base.MyProperty = value;
           }
       }
    }
}

Didn't test this.

Although this feels like a really hack-ish way of doing it. What property are you actually trying to 'have control' over? Since there may be easier ways of doing this.

An example: Change a UserControl so that it's width can't be set between 100 and 200 (Although this is probably a pretty bad way to do it), by hiding it's Width property:

public class MyUserControl : UserControl
{
    public new double Width
    {
        get
        {
            return base.Width;
        }
        set
        {
             if(!(value > 100 && value < 200))
                 base.Width = value;
        }
    }
}
like image 118
K893824 Avatar answered Oct 13 '22 23:10

K893824