Wrox Programmer Forums
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 29th, 2003, 09:25 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default properties

Guys I trying to understand properties,

Using this code I can override the ToString method, and use inheritance, I also have a class which uses properties. This class returns the name of the class when the value is displayed on the screen, can anyone please explain why this is happening??

using System;

class MainEntryPoint
{
    public static void Main(string[] args)
    {
        Money cash = new Money();
        cash.Amount = 40M;
        Console.WriteLine("This is the value" + cash.ToString());
        cash = new BetterMoney();
        Console.WriteLine("Yo Mama" + cash.ToString());
        Console.ReadLine();
    }
}

class Money
{
    private decimal amount;

    public decimal Amount
    {
        get
        {
            return amount;
        }
        set
        {
            amount = value;
        }
    }
}

class BetterMoney : Money
{
    public override string ToString()
    {
        return "$" + Amount.ToString();
    }
}

Thanks

Adz - The World is not enough
__________________
Adz - Learning The J2EE Ways.
 
Old December 29th, 2003, 09:32 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

The default behavior for a class is to return its name when you call the ToString) method. I guess that's the idea behind the sample code: to show you how to override this default behavior.

The Money class will just return its name when you call ToString(). If you want to retrieve its value, use the Amount property:
Code:
Console.WriteLine("This is the value" + cash.Amount.ToString());
This will call the ToString method on the Decimal property Amount. The ToString behavior for a Decimal is to return its value as a string (and so it does not return its name).

Your BetterMoney class does that too. It overrides the ToString() method so it returns a formatted value of the underlying Amount.

Does that make sense?

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old December 29th, 2003, 11:21 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default

Ok,

so even though the Amount of the propertie has been set, cash.ToString of the Money class with return a string of the class name? But if the Amount property was used then the value 40 would have been returned as a string. And the second class over rides this by overiding the string method ToString(). I think thats right??



Adz - The World is not enough
 
Old December 29th, 2003, 11:38 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
oEvent has no properties bradrice BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 0 April 3rd, 2007 08:01 AM
Please help with query properties zachtom Access VBA 1 April 1st, 2004 12:22 PM
Properties robert_83 .NET Web Services 0 July 30th, 2003 07:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.