Question

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(); }
}
Was it helpful?

Solution

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

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top