0

I'm having a problem extending the standard WebControls.Button control. I need to override the text property, but I receive the error message:

cannot override inhereted member 'System.Web.UI.WebControls.Button.Text.get' because it is not marked virtual, abstract or override

I used the following code for a LinkButton, and that worked perfectly:

public class IconLinkButton : LinkButton
{
    private string _icon = "";
    public string Icon
    {
        get
        {
            return _icon;
        }
        set
        {
            _icon = value;
        }
    }

    public override string Text
    {
        get
        {
            return "<i class=\""+Icon+"\"></i> " + base.Text;
        }
        set
        {
            base.Text = value;
        }
    }
}

However, doing the same thing for a standard Button kicks up the error I described above.

public class IconButton : Button
{
    private string _icon = "";
    public string Icon
    {
        get
        {
            return _icon;
        }
        set
        {
            _icon = value;
        }
    }

    public virtual string Text
    {
        get
        {
            return "<i class=\"" + Icon + "\"></i> " + base.Text;
        }
        set
        {
            base.Text = value;
        }
    }
}

How can I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dave
  • 25
  • 1
  • 4

1 Answers1

3

This is because LinkButton has a virtual Text property.. whilst Button does not.

You can hide the base functionality entirely by using new:

public class IconButton : Button {
    public new string Text {
        // implementation
    }
}

Using new hides the inherited member completely.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • Will `base.Text` return the parents' `Text` property or does it override it with its own as well? – Colin Basnett Sep 30 '13 at 22:53
  • It will function as you expect.. since in your original you are just using it as a proxy to the underlying inherited property anyway. – Simon Whitehead Sep 30 '13 at 22:56
  • Thanks Simon, this has worked perfectly and now that you've explained it, it makes sense! Will accept answer as soon as I'm able to :) – Dave Sep 30 '13 at 22:59
  • Keep in mind this method won't be *virtual*. If you have `Button b = new IconButton();`, it will no longer work. – Mike Christensen Sep 30 '13 at 23:02
  • @MikeChristensen I had considered that before answering, however given that it is WebForms and the OP wasn't sure how to even do this.. it was unlikely that was a use case. – Simon Whitehead Sep 30 '13 at 23:04