views:

45

answers:

4

How to override? ToString()

 textBox1.Text.**ToString()**
A: 

To do that you'll need to create a new TextBox that inherits from TextBox and overrides ToString. Have a read here for more info:

How To Override the ToString Method (MSDN)

There may be better ways to do accomplish what you're trying to do though that will be easier to implement if you can specify more details

w69rdy
+1  A: 

Well, to overload the Text property you'd have to inherit from TextBox yourself, which I doubt you want to do

An easier solution would be creating an extension method for string to do what you want:

public static class StringExtensions
{
    ToSpecialString(this string)
    {
         //do your special ToString() here
    }
}
ohadsc
Agreed, extension method is probably best bet.
timothyclifford
you know, if you agree, you *could* upvote ;)
ohadsc
A: 

We are both override :

   class Class1:TextBox
{
    public Class1()
    {

       // this.Text.ToString();
    }

    public override string ToString()
    {
        return ("mystring");
    }

    //protected override Text.tostrong()
    //{

    //}
}
textBox
You are overriding TextBox.Tostring(), where you should be overriding TextBox.Text. And you don't need to call ToString - Text is already a string !
ohadsc
+1  A: 

Why on earth would you want to? Text is already a string.

If you need to format the string differently, use String.Format(...), or a custom method you don't need to override the behavior.

Doobi