473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ToString()

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?
Nov 16 '05 #1
6 3340
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" <an*******@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
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?

Nov 16 '05 #2

"RonG" wrote...
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
Nov 16 '05 #3
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.
Nov 16 '05 #4
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.
Nov 16 '05 #5

"RonG" wrote...
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


Nov 16 '05 #6
Your right. That work
Thank you
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34...
6
by: Joe | last post by:
Stupid question, but I'm really stuck I have a class that overrides ToString(). When this class is cast back to Object, and ToString() called, Object.ToString() is called instead. I've tried...
8
by: John Cobb | last post by:
Excuse me if I use some terminology incorrectly as I am not well versed in Object Orientation. In short I want to create an Enumerated type similar to the following Enum Monitor as Byte Small...
0
by: bcobra | last post by:
What am I doing wrong? the code: age Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <%@ Register TagPrefix="MM" Namespace="DreamweaverCtrls"...
7
by: Dylan Parry | last post by:
Hi folks, I was wondering if there is any way of formatting a date to include the ordinal characters? I've looked at the documentation for DateTime.ToString(), but no where can I find...
2
by: archana | last post by:
Hi all, I am facing some wired problem while using above mention data type. What i am doing is i am writing DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" +...
1
by: ivansh | last post by:
Hello, For one java class (Hello) i use another (HelloPrinter) to build the string representation of the first one. When i've tried to use this from within jython, HelloPrinter.toString(hello)...
8
by: Phil Jollans | last post by:
Hi, I am having difficulty overriding the ToString() method of CultureInfo using Visual Studio 2005. Exactly the same code works fine with Visual Studio .NET 2003. What I am doing is adding...
2
by: Berryl Hesh | last post by:
I'm interested in how experienced.Net developers might handle routine display tasks as a general strategy. Let's say you have a use in your domain for value objects that encapsulate a person's...
19
by: Michael C | last post by:
If we have something like this object x = null; MessageBox.Show(x.ToString()) we get an error. That kindof makes sense but there is no reason the behaviour couldn't be to return an empty...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.