Home  |  Delphi .net Home  |  System.Enum  |  ToString Method
  
ToString  
Method  
Converts the current Enum value to a string
Enum Class
System NameSpace
CF1.  Function ToString : String;
NotCF2.  Function ToString ( Formatting : String ) : String;
NotCF3.  Function ToString ( Formatting : String; FormatProvider : IFormatProvider ) : String;
NotCF4.  Function ToString ( FormatProvider : IFormatProvider ) : String;
CF : Methods with this mark are Compact Framework Compatible
Description
The ToString method converts the current Enum value into a string representation.
 
The Formatting string (or array of strings) provides the desired formatting of the enum value.
 
The formatting value has the following syntax :
 
FormattingChar[Precision]
 
The formatting character can be one of the following values :
 
D or d Integer formatting such as 123456
F or f Fixed Point such as 1234.56
G or g General : type dependent
X or x Hex such as E06FC (using X) or e06fc (using x)

 
The FormatProvider parameter determines the parsing rules and is beyond the scope of this article.
Notes
ToString without parameters is a method inherited from the base System.Object (TObject) class. It provides a ready method for classes to convert their content into a string format.
Microsoft MSDN Links
System
System.Enum
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Simple enum and a flags enum illustrated
program Project1;
{$APPTYPE CONSOLE}

type
  // A simple enum
  Attributes = (Tall = 1, Fat    = 2, Blonde   = 4);

  // An enum treated as a set
  [FlagsAttribute]
  Qualities  = (Sexy = 1, Clever = 2, Handsome = 4);

var
  Brian : System.Enum;
  Paul  : System.Enum;
  Fred  : System.Enum;
  Jim   : System.Enum;

begin
  // Using a simple enum
  Brian := Fat;
  Console.WriteLine('Brian is ' + Brian.ToString);

  // Using a simple enum incorrectly
  Paul  := Tall or Fat;
  Console.WriteLine('Paul  is ' + Paul.ToString);

  // Using a flags enum as an enum
  Fred  := Clever;
  Console.WriteLine('Fred  is ' + Fred.ToString);

  // Using a flags enum as a set
  Jim   := Sexy or Clever;
  Console.WriteLine('Jim   is ' + Jim.ToString);

  Console.ReadLine;
end.
   Brian is Fat
   Paul  is 3
   Fred  is Clever
   Jim   is Sexy, Clever
Illustrating the formatting character options
program Project1;
{$APPTYPE CONSOLE}

var
  valueA : System.Enum;

begin
  valueA := DayOfWeek.Friday;

  Console.WriteLine('    Default    = {0}', valueA.ToString);
  Console.WriteLine('D : Integer    = {0}', valueA.ToString('D'));
  Console.WriteLine('F : Fixed      = {0}', valueA.ToString('F'));
  Console.WriteLine('G : General    = {0}', valueA.ToString('G'));
  Console.WriteLine('X : Hex        = {0}', valueA.ToString('X'));

  Console.ReadLine;
end.
       Default    = Friday
   D : Integer    = 5
   F : Fixed      = Friday
   G : General    = Friday
   X : Hex        = 00000005
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author