ToString()

G

Guest

This might seem like a stupid question but I'd really appreciate an answer

TextBox.Text is property yet it has a ToString() method. In other words, i can type in textBox1.Text.ToString() and it returns the Text property.

How can a property have a method
Why doesn't this show up in the documentation
Can it be overridden?
 
J

John Scalco

Ron,
The reason there is a ToString() method for a property is because all object
(they inherit from system.object) implement method called ToString(). So
even the simplest object has a ToString() method.

It can be overridden. For example, suppose you created a Customer class, and
you wanted ToString() to return a nicely formatted string, maybe first_name,
last_name or something like that, you could override ToString() in your
class and when someone used your customer class they could call tostring()
with your overridden version.

ex:

Customer cust = new Customer("Fred", "Flintstone", "03/02/1990");
MessageBox.Show ("The Customers name is " + cust.ToString());

hope that helps,
Sincerely,
John Scalco

RonG said:
This might seem like a stupid question but I'd really appreciate an answer.

TextBox.Text is property yet it has a ToString() method. In other words, i
can type in textBox1.Text.ToString() and it returns the Text property.
 
B

Bjorn Abelli

...
TextBox.Text is property yet it has a ToString() method.

No, it actually doesn't.

You could see a property as a "shortcut" for what we in OO-terms calls
"accessors" and "mutators", methods to get or set values of fields within an
object (though it can be used for other things as well).

As such TextBox.Text *return* an object of type string, which in turn has
the method ToString.
In other words, i can type in textBox1.Text.ToString()
and it returns the Text property.

No, textBox1.Text returns the string object that it represents. ToString can
hence be invoked on that string.

However, in this particular example it's overkill, as what the ToString
method of a string returns is the string itself...
How can a property have a method?

As I said, it doesn't.
Why doesn't this show up in the documentation?

As it isn't a behavior of the *property*, but of the object it returns.
Can it be overridden?

If you write your own properties, you decide what class the object is of for
your property to return.

If you have defined that class yourself, you can override ToString in that
class.

// Bjorn A
 
G

Guest

I understand that I can override ToString() in most instances but I'm specifically referring to the ToString method TextBox.Text.

Are you saying that a property is an object? If so, then there must be a class Text that has a ToString method i can override but i don't see one in the documentation. If I wanted to override the ToString method of Textbox itself, no problem. I can see it is a method called from the base class

I am perfectly willing to admit I might be missing something here but I don't see what it is.
 
G

Guest

I get it
So in this case, since the property(Text) is a string, i would need to override the ToString method of String.

Thank you.
 
B

Bjorn Abelli

...
So in this case, since the property(Text) is a string, i would
need to override the ToString method of String.

The System.String class is sealed, so you can't inherit from it and hence
not override it.

However, I've got the feeling that what you really want to do is to change
what the *property* Text returns.

That you can do, by subclassing the TextBox class, and override the Text
property, and hence use your specialized TextBox in your forms.

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top