views:

55

answers:

1

C# in VS2005: can you overload the .ToString() method of a property?

+5  A: 

No - properties have a get method, which returns a value -- you can overload the implementation of ToString() on the type of that value, which will allow you to do:

myClass.MyProperty.ToString()

which is likely what you're looking for. Simply define a function such as:

public override string ToString() {
    // return string version of value
}

in that type's class.

Blair Holloway
That is unfortunate. A Property is like a wrapper object for a type, so I think you should be able to overload methods like ToString() to customise the string representation of a Property.
Craig Johnston