Category Archives: Utilities

Cinchoo – ChoObject.ToString(), Part 1

Cinchoo framework provides one of the very useful method to automatic generation of string represent of an object in very well formatted way.

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core

There are two ways, you can declare the type

Method 1:

Declare a type with public members, use ChoObject.ToString() method to get the string represents as below

public class Security
{
    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(ChoObject.ToString(security));
    }
    finally
    {
        ChoAppDomain.Exit();
    }
}

The output is

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

Press any key to continue . . .

Method 2:

Declare a type derived from ChoObject with public members, use ToString() instance method directly to get the string represents as below

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 . . .

Happy coding!!!

Cinchoo – ChoObject.ToString(), Part 2

In case where you want to provide your own string represent for a type, you can do so by overriding ToString() method. Cinchoo framework will use that overload and skip its own implementation to get the string represent of the type.

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;
    }

    public override string ToString()
    {
        return String.Format("Symbol: {0}, Company: {1}, Price: {2}", Symbol, CompanyName, 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 will be

Symbol: AAPL, Company: Apple Inc, Price: 457.25

Press any key to continue . . .

Happy coding!!!

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!!!

Cinchoo – ChoObject.ToString(), Part 4

Handling Array Members

Cinchoo framework handles Array/List<T> members while constructing string represent of a type.

For a type below having array member

[ChoTypeFormatter("'{this.Name}' Portfolio State")]
public class Portfolio : ChoObject
{
    public string Name;
    public string Description;
    public string[] Securities;

    public Portfolio(string name, string description, string[] securities)
    {
        Name = name;
        Description = description;
        Securities = securities;
    }
}
static void Main(string[] args)
{
    try
    {
        Portfolio portfolio = new Portfolio("High-Growth", "High Growth Portfolio", new string[] { "AAPL", "GOOG", "IBM" });
        Console.WriteLine(portfolio.ToString());
    }
    finally
    {
        ChoAppDomain.Exit();
    }
}

The output will be

-- 'High-Growth' Portfolio State --
        Name: High-Growth
        Description: High Growth Portfolio
        Securities [Length: 3]:
                AAPL
                GOOG
                IBM

Press any key to continue . . .

You can override the default behavior by providing your own formatter. I’ll talk about creating one and using it in a type to customize it.

Happy coding!!!

Cinchoo – ChoObject.ToString(), Part 5

Handling of nested types

Cinchoo framework handles nested custom members while constructing string represent of a type.

For a type below having array member

[ChoTypeFormatter("'{this.Name}' Portfolio State")]
public class Portfolio : ChoObject
{
    public string Name;
    public string Description;
    public Security Security;

    public Portfolio(string name, string description, Security security)
    {
        Name = name;
        Description = description;
        Security = security;
    }
}
static void Main(string[] args)
{
    try
    {
        Portfolio portfolio = new Portfolio("High-Growth", "High Growth Portfolio", new Security("AAPL", "Apple Inc", 458.25));
        Console.WriteLine(portfolio.ToString());
    }
    finally
    {
        ChoAppDomain.Exit();
    }
}

The output will be

-- 'High-Growth' Portfolio State --
        Name: High-Growth
        Description: High Growth Portfolio
        Security:
                -- 'AAPL' Security State --
                        Symbol: AAPL
                        CompanyName: Apple Inc
                        Price: 458.25
                        x [Length: 3]:
                                AAPL1
                                APPL2
                                APPL3

Press any key to continue . . .

You can override the default behavior by providing your own formatter. I’ll talk about creating one and using it in a type to customize it.

Happy coding!!!

Cinchoo – ChoObject.ToString(), Part 6

Handling of Dictionary members

Cinchoo framework seamlessly support the dictionary members while constructing string represent of a type.

For a type below having dictionary member

[ChoTypeFormatter("'{this.Name}' Security State")]
public class Portfolio : ChoObject
{
    public string Name;
    public string Description;
    public Dictionary<string, Security> Securities;

    public Portfolio(string name, string description, Security[] securities)
    {
        Name = name;
        Description = description;
        Securities = new Dictionary<string, Security>();

        foreach (Security sec in securities)
            Securities.Add(sec.Symbol, sec);
    }
}

static void Main(string[] args)
{
    try
    {
        Security sec1 = new Security("AAPL", "Apple Inc", 458.25);
        Security sec2 = new Security("GOOG", "Google Inc", 658.25);
        Security sec3 = new Security("IBM", "International Business Machines Inc", 198.50);

        Portfolio portfolio = new Portfolio("High-Growth", "High Growth Portfolio", new Security[] { sec1, sec2, sec3 });
        Console.WriteLine(portfolio.ToString());
    }
    finally
    {
        ChoAppDomain.Exit();
    }
}

The output will be

-- 'High-Growth' Security State --
	Name: High-Growth
	Description: High Growth Portfolio
	Securities [Length: 3]:
		-- System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Cinchoo.Core.Test.Security, Cinchoo.Core.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] Dump --
			Key: AAPL
			Value:
				-- 'AAPL' Security State --
					Symbol: AAPL
					CompanyName: Apple Inc
					Price: 458.25
					x [Length: 3]:
						AAPL1
						APPL2
						APPL3

		-- System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Cinchoo.Core.Test.Security, Cinchoo.Core.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] Dump --
			Key: GOOG
			Value:
				-- 'GOOG' Security State --
					Symbol: GOOG
					CompanyName: Google Inc
					Price: 658.25
					x [Length: 3]:
						AAPL1
						APPL2
						APPL3

		-- System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Cinchoo.Core.Test.Security, Cinchoo.Core.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] Dump --
			Key: IBM
			Value:
				-- 'IBM' Security State --
					Symbol: IBM
					CompanyName: International Business Machines Inc
					Price: 198.5
					x [Length: 3]:
						AAPL1
						APPL2
						APPL3

Press any key to continue . . .

If you want to simplify the title of each security item to ‘Security’, you can do so by declaring ChoMemberItemFormatter attribute (a attribute used to format each item in a collection) as below

[ChoTypeFormatter("'{this.Name}' Security State")]
public class Portfolio : ChoObject
{
    public string Name;
    public string Description;
    [ChoMemberItemFormatter("Security")]
    public Dictionary<string, Security> Securities;

    public Portfolio(string name, string description, Security[] securities)
    {
        Name = name;
        Description = description;
        Securities = new Dictionary<string, Security>();

        foreach (Security sec in securities)
            Securities.Add(sec.Symbol, sec);
    }
}

Output will be

-- 'High-Growth' Security State --
	Name: High-Growth
	Description: High Growth Portfolio
	Securities [Length: 3]:
		-- Security --
			Key: AAPL
			Value:
				-- 'AAPL' Security State --
					Symbol: AAPL
					CompanyName: Apple Inc
					Price: 458.25
					x [Length: 3]:
						AAPL1
						APPL2
						APPL3

		-- Security --
			Key: GOOG
			Value:
				-- 'GOOG' Security State --
					Symbol: GOOG
					CompanyName: Google Inc
					Price: 658.25
					x [Length: 3]:
						AAPL1
						APPL2
						APPL3

		-- Security --
			Key: IBM
			Value:
				-- 'IBM' Security State --
					Symbol: IBM
					CompanyName: International Business Machines Inc
					Price: 198.5
					x [Length: 3]:
						AAPL1
						APPL2
						APPL3

Happy coding!!!

Cinchoo – ChoObject.ToString(), Part 7

Customizing the member names

When constructing automatic string represent of an object, Cinchoo framework uses the member name as a title for each member. You can override this behavior by decorating each member with ChoMemberFormatterAttribute.

[ChoTypeFormatter("'{this.Symbol}' Security State")]
public class Security : ChoObject
{
    public string Symbol;
    public string CompanyName;
    [ChoMemberFormatter("Last Price")]
    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 will be

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

Happy coding!!!

Cinchoo – ChoObject.ToString(), Part 8

Cinchoo framework works very well with derived classes while generating string represent. Here I’ll walk you over with a sample

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

    public Security(string symbol, string companyName, double price)
    {
        Symbol = symbol;
        CompanyName = companyName;
        Price = price;
    }
}

[ChoTypeFormatter("'{this.Symbol}' Stock State")]
public class Stock : Security
{
    public string Exchange;

    public Stock(string symbol, string companyName, double price)
        : base(symbol, companyName, price)
    {
    }
}

static void Main(string[] args)
{
    try
    {
        Stock security = new Stock("AAPL", "Apple Inc", 457.25);
        security.Exchange = "NYSE";
        Console.WriteLine(security.ToString());
    }
    finally
    {
        ChoAppDomain.Exit();
    }
}

The output will be

-- 'AAPL' Stock State --
        Exchange: NYSE
        Symbol: AAPL
        CompanyName: Apple Inc
        Last Price: 457.25

Happy coding!!!

Cinchoo – ChoObject.ToString(), Part 9

Ignoring members

An object members decorated with ChoMemberFormatterIgnore will be ignored by Cinchoo framework while generating string represent. Here I’ll walk you over with a sample

[ChoTypeFormatter("'{this.Symbol}' Security State")]
public class Security : ChoObject
{
    public string Symbol;
    public string CompanyName;
    [ChoMemberFormatterIgnore]
    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 will be

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

Press any key to continue . . .

Happy coding!!!

Cinchoo – ChoObject.ToString(), Part 10

Defining and using TypeConverter

Sometime you may want to customize the object member string representation. One way of achieving it by using TypeConverters. Either you can create a new TypeConverter or use the existing .NET type converters.

Defining TypeConverter

 Please visit MSDN on How to: Implement a Type Converter. Below is the sample converter
public class ChoArrayToStringConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(string))
            return true;
        else
            return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (value == null)
            return null;

        StringBuilder msg = new StringBuilder();
        if (value.GetType().IsArray)
        {
            foreach (object item in (Array)value)
            {
                if (msg.Length == 0)
                    msg.Append(ChoString.ToString(item, String.Empty, String.Empty));
                else
                    msg.AppendFormat(", {0}", ChoString.ToString(item, String.Empty, String.Empty));
            }
            return msg.ToString();
        }
        return base.ConvertFrom(context, culture, value);
    }
}

Please visit .NET TypeConverters for the list of Type Converters that .NET provides. Those can be used in here in converting members to string represent.

Using TypeConverter

[ChoTypeFormatter("'{this.Name}' Security State")]
public class Portfolio : ChoObject
{
    public string Name;
    public string Description;
    [ChoMemberFormatter(TypeConverter = typeof(ChoArrayToStringConverter))]
    public string[] Securities;
    [ChoMemberFormatter(TypeConverter = typeof(BooleanConverter))]
    public bool Live;

    public Portfolio(string name, string description, string[] securities)
    {
        Name = name;
        Description = description;
        Securities = securities;
    }
}

static void Main(string[] args)
{
    try
    {
        Portfolio portfolio = new Portfolio("High-Growth", "High Growth Portfolio", new string[] { "AAPL", "GOOG", "IBM" });
        Console.WriteLine(portfolio.ToString());
    }
    finally
    {
        ChoAppDomain.Exit();
    }
}

The output will be

-- 'High-Growth' Security State --
        Name: High-Growth
        Description: High Growth Portfolio
        Securities: AAPL, GOOG, IBM
        Live: False

Press any key to continue . . .

Happy coding!!!