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? 6 3354
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?
"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
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.
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.
"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
Your right. That work
Thank you This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
123.0000 123
i.e. I want to trim trailing zeros and (decimal point if no decimal values
|
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 everything:
Public Overrides Function ToString() as String,
Overrides Function ToString() as String,
Overrides Overloads ToString() as string,...
|
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
Medium
Large
End Enum
|
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"
Assembly="DreamweaverCtrls,version=1.0.0.0,publicKeyToken=836f606ede05d46a,culture=neutral"
%>
<MM:DataSet
id="SSF2Pinky"
|
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 information on ordinals.
For example, I currently use:
Console.WriteLine(myDate.ToString("d MMMM, yyyy"));
| |
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() +
":" + DateTime.Now.Second.ToString() +
":"+DateTime.Now.Millisecond.ToString()
to the file.
|
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) call gives results
like Object.toString() of hello has being called. The example below
shows this behaviour.
Could somebody explain this?
|
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 objects which are derived from CultureInfo to a
ListBox. I want the language name to be displayed in the native language, so
I override the...
|
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 Name, for example.
Would anyone write a custom format provider to display the last name first
("Phelps, Michael"), or first name first ("Michael...
|
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 string. When we call x.ToString we
are really calling a function like this:
class Object
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |