Calling base Text method on custom TextBox

Posted by The Demigeek on Stack Overflow See other posts from Stack Overflow or by The Demigeek
Published on 2009-07-13T18:22:04Z Indexed on 2010/04/13 22:03 UTC
Read the original article Hit count: 188

Filed under:

I'm trying to create a CurrencyTextBox that inherits from TextBox. I'm seeing some really weird behavior that I just don't understand.

After lots of testing, I think I can summarize as follows:

In the class code, when I access base.Text (to get the textbox's text), I'm actually getting the return value of my overridden Text property.

I thought the base keyword would ensure that the underlying object's methods get called.

To demonstrate:


public class cTestTextBox : System.Windows.Forms.TextBox
    {
        string strText = "";

        public cTestTextBox()
        {
            SetVal("AAA");
            base.Text = "TEST";
        }
        public override string Text
        {
            get
            {
                string s = strText;
                s = "++" + s + "++";
                return s;
            }
        }
        public void SetVal(string val)
        {
            strText = val;
        }
    }

Place this control on a form and set a breakpoint on the constructor. Run the app. Hover your mouse over the base.Text expression. Note that the tooltip shows you the value of the overridden property, not the base property. Execute the SetVal() statement and again hover your mouse over the base.Text expression. Note that the tooltop shows you the value of the overridden property, not the base property.

How do I reliably access the Text property of the textbox from which I'm inheriting?

© Stack Overflow or respective owner

Related posts about inheritance