ToString ()

Discussion in 'C#' started by arunlalds, Mar 13, 2010.

  1. arunlalds

    arunlalds Banned

    Joined:
    Mar 12, 2010
    Messages:
    43
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    student
    Location:
    India
    To get a simple string representation of the vertex, override ToString() to return a
    string of your choosing.
    Code:
    public override string ToString()
    {
    return string.Format(“({0}, {1}, {2})”, X, Y, Z);
    }
    The code
    Vertex3d v = new Vertex3d(1.0, 2.0, 3.0);
    Trace.WriteLine(v.ToString());
    
    produces the following output:
    (1, 2, 3)
    Implement Custom Formatting for Fine Control
    Although the ToString()implementation gets the job done, and is especially
    handy for debugging (Visual Studio will automatically call ToString() on
    objects in the debugger windows), it is not very flexible. By implementing
    IFormattable on your type, you can create a version of ToString() that is as
    flexible as you need.
    Let’s create a simple format syntax that allows us to specify which of the three values
    to print. To do this, we’ll define the following format string:
    “X, Y”
    This tells Vertex3d to print out X and Y. The comma and space (and any other character)
    will be output as-is.
    The struct definition will now be as follows:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace VertexDemo
    {
    struct Vertex3d : IFormattable
    {
    ...
    public string ToString(string format, IFormatProvider formatProvider)
    {
    //”G” is .Net’s standard for general formatting--all
    //types should support it
    if (format == null) format = “G”;
    // is the user providing their own format provider?
    if (formatProvider != null)
    {
    ICustomFormatter formatter =
    formatProvider.GetFormat(this.GetType())
    as ICustomFormatter;
    if (formatter != null)
    {
    return formatter.Format(format, this, formatProvider);
    }
    }
    //formatting is up to us, so let’s do it
    if (format == “G”)
    {
    return string.Format(“({0}, {1}, {2})”, X, Y, Z);
    }
    StringBuilder sb = new StringBuilder();
    int sourceIndex = 0;
    while (sourceIndex < format.Length)
    {
    switch (format[sourceIndex])
    {
    case ‘X’:
    sb.Append(X.ToString());
    break;
    case ‘Y’:
    sb.Append(Y.ToString());
    break;
    case ‘Z’:
    sb.Append(Z.ToString());
    break;
    default:
    sb.Append(format[sourceIndex]);
    break;
    }
    sourceIndex++;
    }
    return sb.ToString();
    }
    }
    }
    
    Formatting with ICustomFormatter and StringBuilder
    Use ICustomFormatter and StringBuilder. This example prints out
    type information, as well as whatever the custom format string specifies for the
    given types.
    Code:
    class TypeFormatter : IFormatProvider, ICustomFormatter
    {
    public object GetFormat(Type formatType)
    {
    if (formatType == typeof(ICustomFormatter)) return this;
    return Thread.CurrentThread.CurrentCulture.GetFormat(formatType);
    }
    public string Format(string format, object arg, IFormatProvider
    formatProvider)
    {
    string value;
    IFormattable formattable = arg as IFormattable;
    if (formattable == null)
    {
    value = arg.ToString();
    }
    else
    {
    value = formattable.ToString(format, formatProvider);
    }
    return string.Format(“Type: {0}, Value: {1}”, arg.GetType(),
    value);
    }
    }
    
    The class can be used like this:
    Code:
    Vertex3d v = new Vertex3d(1.0, 2.0, 3.0);
    Vertex3d v2 = new Vertex3d(4.0, 5.0, 6.0);
    TypeFormatter formatter = new TypeFormatter();
    StringBuilder sb = new StringBuilder();
    sb.AppendFormat(formatter, “{0:(X Y)}; {1:[X, Y, Z]}”, v, v2);
    Console.WriteLine(sb.ToString());
    
    The following output is produced:
    Code:
    Type: ch02.Vertex3d, Value: (1 2); Type: ch02.Vertex3d, Value: [4, 5, 6]
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice