views:

196

answers:

3

I am extending TextBox WebControl to work as a sort of "DateTextBox" which exposes it's value as a property (DateValue) in the code-behind:

public sealed class DateTextBox : TextBox
{
    public DateTime ?DateValue
    {
        /* ToDateFromUserInterface() and ToUserInterfaceString() are both
           extension methods */

        get
        {
            return
            (
                String.IsNullOrEmpty(Text) ?
                new DateTime?() :
                Text.ToDateFromUserInterface()
            );
        }

        set
        {
            if (value != null) Text = ((DateTime)value).ToUserInterfaceString();
        }
    }
}

Given the fact that this control is only supposed to be used with dates, there's no reason for it to inherrit the Text property from it's parrent.

Is there any way to hide it?.. Other than implementing a NotImplementedException, like so:

new public String Text
{
    get { throw new NotImplementedException(); }
    set { throw new NotImplementedException(); }
}
+3  A: 

just override it and provide a string version of the date. Any time you inherit from a base class, it's usually a bad idea to try to change the existing interface like this ... it goes against all object oriented practices :-P

Joel Martinez
+1 I would do the same (and I'm implementing it this way)
Arthur
A: 

If you are creating a class that inherits another class, all the properties of the parent method should still be relevant. In this case, you could return a text representation of the date for the get, and parse a text representation for the set.

Mike
+1  A: 

You can use EditorBrowsable attribute. It will only hide the property from Intelisense, but won't break compile time compatibility with base class.

Although you can still use the property if you know it's name.

PS: Read user comments at the end of this article. http://msdn.microsoft.com/en-us/library/system.componentmodel.editorbrowsableattribute.aspx

Alex Reitbort
Does this hide properties from Intellisense in the code-behind view?
roosteronacid
As long as the class defined in different assembly you shouldn't see the property in Intellisense . But you can easily test it.
Alex Reitbort