Well, yes.
A class contains a default implementation for ToString() which it inherits from Object. This default behavior is to return the name of the class.
When you call ToString on the Money object, how would the run-time know you'd want to return the value of the Amount property instead? It doesn't, so all it does is still return the name of the class. The Amount property (itself another object as well) has also a method called ToString(). When you run that method, it will return its internal value as a string.
In the BetterMoney class the ToString method is overridden to have it return something more useful. If you want, you could do the same for your Money class:
Code:
class Money
{
private decimal amount;
public decimal Amount
{
get
{
return amount;
}
set
{
amount = value;
}
}
public override string ToString()
{
if (amount <= 0)
{
return "Whoops, you're not too rich ;)";
}
else
{
return "Your amount is " + amount.ToString();
}
}
}
This code overrides the ToString method of Object, so the string it returns is a bit more useful. It also calls the ToString method of the Amount variable in case the amount is more than zero.
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.