Cinchoo – ChoObject.ToString(), Part 3

Here I’ll show you on how to customize the type header of a type on string represent while calling ToString() override.

Below sample displays the standard header when calling ToString()

public class Security : ChoObject
{
    public string Symbol;
    public string CompanyName;
    public double Price;

    public Security(string symbol, string companyName, double price)
    {
        Symbol = symbol;
        CompanyName = companyName;
        Price = price;
    }
}
static void Main(string[] args)
{
    try
    {
        Security security = new Security("AAPL", "Apple Inc", 457.25);
        Console.WriteLine(security.ToString());
    }
    finally
    {
        ChoAppDomain.Exit();
    }
}

The output is

-- Cinchoo.Core.Test.Security Dump --
        Symbol: AAPL
        CompanyName: Apple Inc
        Price: 457.25

Press any key to continue . . .

Let say, you want to customize the header as ‘Security State’ while displaying the string represent. It can be done as below

[ChoTypeFormatter("Security State")]
public class Security : ChoObject
{
    public string Symbol;
    public string CompanyName;
    public double Price;

    public Security(string symbol, string companyName, double price)
    {
        Symbol = symbol;
        CompanyName = companyName;
        Price = price;
    }
}
static void Main(string[] args)
{
    try
    {
        Security security = new Security("AAPL", "Apple Inc", 457.25);
        Console.WriteLine(security.ToString());
    }
    finally
    {
        ChoAppDomain.Exit();
    }
}

The output is

-- Security State --
        Symbol: AAPL
        CompanyName: Apple Inc
        Price: 457.25

Press any key to continue . . .

In some cases, where you want to provide context information in the header, lets say, the object Symbol value to be displayed as part of the header, it can done as below

[ChoTypeFormatter("'{this.Symbol}' Security State")]
public class Security : ChoObject
{
    public string Symbol;
    public string CompanyName;
    public double Price;

    public Security(string symbol, string companyName, double price)
    {
        Symbol = symbol;
        CompanyName = companyName;
        Price = price;
    }
}
static void Main(string[] args)
{
    try
    {
        Security security = new Security("AAPL", "Apple Inc", 457.25);
        Console.WriteLine(security.ToString());
    }
    finally
    {
        ChoAppDomain.Exit();
    }
}

The output is

-- 'AAPL' Security State --
        Symbol: AAPL
        CompanyName: Apple Inc
        Price: 457.25

Press any key to continue . . .

Happy coding!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s