Override a property defined in base class

24,353

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;
        }
    }
}
Share:
24,353
Ajai
Author by

Ajai

By Day: Tinker code at Google By Night: Sound sleeper Other times: Live life? -_-

Updated on July 09, 2022

Comments

  • Ajai
    Ajai almost 2 years

    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.

    • Tigran
      Tigran about 12 years
      what is the meaning of MyUserControl newControl = new MyUserControl(); this line in code provided?
    • Kevin Gosse
      Kevin Gosse about 12 years
      One vote for the ASCII diagram :D If the base property isn't set as 'virtual', you have no way to override it other than new (with the drawback you've mentioned). But maybe you can reach your goal another way. Which property are you trying to override? Also, you can wrap your control inside of a custom control, and set the properties as you wish.
    • Henk Holterman
      Henk Holterman about 12 years
      Since it's XAML, is this about a Dependency or Attached Property? It's quite a picture but you'll need to provide some more code too.
    • Henk Holterman
      Henk Holterman about 12 years
      UIElement is a library class so please just name SomeProperty. Then someone can look up the specs. The answer: it depends.
    • Ajai
      Ajai about 12 years
      @Tigran: MyUserControl newControl = new MyUserControl(); signifies that I am already having a control in my xaml form and that I am creating a new object for MyCustomeControl and assigning it with the control that I added in xaml.
    • Ajai
      Ajai about 12 years
      @all: I have no control over the base classes that I had mentioned.. So all I can do is to play with my MyUserControl class.
    • Ajai
      Ajai about 12 years
      @HenkHolterman: I am trying to create a new webbrowser control and I am trying to override isHitTestVisible property in UIElement class. I want to override it because I need to check something before assigning it.
    • Ajai
      Ajai about 12 years
      @KooKiz I want to override the isHitTestVisible property for the webbrowser control defined in UIElement class.
    • Henk Holterman
      Henk Holterman about 12 years
    • Kevin Gosse
      Kevin Gosse about 12 years
      @HenkHolterman It isn't a duplicate. You can't override a dependency property's metadata with WP7, so he can't use the same solution.
    • Henk Holterman
      Henk Holterman about 12 years
      @kookiz - you're right, I overlooked the WP7 tag.
  • Kevin Gosse
    Kevin Gosse about 12 years
    That is, if the base method is virtual or abstract.
  • mircea
    mircea about 12 years
    You should use "new" instead of "override". "override" is used for "virtual" properties.
  • Ajai
    Ajai about 12 years
    I have no control over the base classes. I can just do my changes in MyUserControl class..
  • K893824
    K893824 about 12 years
    You don't need any control over the base classes. See my example. You just 'hide' the base classes' property and then create a sort of 'wrapper' for it, adding whatever you want.
  • Ajai
    Ajai about 12 years
    Yes I actually got it now! Thanks for the suggestion.
  • K893824
    K893824 about 12 years
    No problem, glad I could help.
  • Henk Holterman
    Henk Holterman about 12 years
    Just a pity it makes no sense (at all) for a Dependency property.
  • Kevin Gosse
    Kevin Gosse about 12 years
    But note that if the user changes the property by using the base class, you won't be notified: ((UIElement)yourControl).IsHitTestVisible = false;
  • Ajai
    Ajai about 12 years
    @KooKiz: That is interesting. But I just now tried it and I was not able to assign the property without without skipping my logic.
  • Admin
    Admin about 12 years
    Wow, that is not advisable; its an awful hack. Solution should be to not name your property the same as one in the base class.
  • Ajai
    Ajai about 12 years
    @Will: I agree with you. This might not be the best design decision for designing a control. But I deliberately need to override it as I am not the one who designed the base class and I need to check for a few conditions before setting that one particular property.
  • IAbstract
    IAbstract about 12 years
    Using the new keyword is not overriding the member, it is hiding the member - big difference. And, as @Will states in a comment above ...*'that is not advisable'*
  • IAbstract
    IAbstract about 12 years
    I am completely with @Will: at least you should rename the method/property.
  • Chris McCowan
    Chris McCowan over 4 years
    Common problems are 1) base functions will still access the base property. 2) BaseClass thing = new ChildClass(); thing.Age = 2; // this acts on BaseClass.Age not ChildClass.Age