Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create my own custom ToString() format?

I would like to specify the format of the ToString format, but I am not sure of the best way to handle this.

For example if I have the following specifiers

  • EE = equipment
  • ED = equipment description
  • EI = equipment ID

so that if I used the ToString as such:

eqp.ToString("EE-EI (ED)")

the output might be:

"CAT994-61 (Front end loader)"

Would the best way be to search for the substrings and do a token replacement? Does any one have an example of doing this?

I'm currently doing sequential string.Replace, which works nicely.

public class Equipment
{
    // (other class code)

    public string ToString(string format)
    {
        string output = format;
        output = output.Replace("EE", _EquipID);
        output = output.Replace("ED", _EquipDescription);
        output = output.Replace("DI", _DepartID);
        return output;
    }

    public override string ToString()
    {
        return _EquipID;
    }
}
like image 251
fishhead Avatar asked Feb 07 '10 23:02

fishhead


People also ask

How do I create a ToString method in Visual Studio?

To use it just put your cursor on the class name and click on the lightbulb or "Ctrl + .", between the several option there will be "Generate ToString()".

What does ToString () do in C#?

ToString is the major formatting method in the . NET Framework. It converts an object to its string representation so that it is suitable for display.

Why should you override the ToString () method?

When you create a custom class or struct, you should override the ToString method in order to provide information about your type to client code. For information about how to use format strings and other types of custom formatting with the ToString method, see Formatting Types.

What is ToString and string format?

The ToString method uses the standard or custom numeric format string specified by the format parameter to convert the value of the current instance into its string representation.


2 Answers

Combining string format, and overloading the ToString you can do this:

public override string ToString(string myFormat)
{
    myFormat = myFormat.Replace("EE", "{0}");
    myFormat = myFormat.Replace("EI", "{1}");
    myFormat = myFormat.Replace("ED", "{2}");
    return String.Format(myFormat, this.equipment, this.description, this.id);
}

You can also use regular expressions to make the Replace functions nicer.


This can be used as follows:

oProd.ToString("EE,ED,EI");
oProd.ToString("EE-ED (EI)"); // change order
oProd.ToString("ED-EE,EE,EE (EI)"); // multiple times
oProd.ToString("ED:EI"); // have some missing
etc

The String.Format gives you the flexibility to position the format variables any way like, have them multiple times, or drop some.

like image 83
Amirshk Avatar answered Sep 21 '22 04:09

Amirshk


First override ToString()

Then if you want it to be dynamic you would have to parse the format string a token at a time and add the appropriate string to your output string. Using single letter format specifiers would make it a little simpler since you could parse it a character at a time. If it is a special character you output the appropriate data else output the character.

So something like this

public override string ToString(string format)
{
  StringBuilder s = new StringBuilder();

  foreach (char c in format)
  {
    switch (c)
    {
      case 'E':
        s.Append(EquipID);
        break;
      case 'D':
        s.Append(EquipDesc);
        break;
      case 'I':
        s.Append(DepartID);
        break;
      default:
        s.Append(c);
        break;
    }
  }

  return s.ToString();
}

This has the relatively minor advantage of doing it in one pass which is more efficient than multiple string replace calls.

like image 28
Kevin Gale Avatar answered Sep 25 '22 04:09

Kevin Gale