Home  |  Delphi .net Home  |  System.Enum  |  Format Method
  
Format  
Method  
Formats the current Enum value into a string
Enum Class
System NameSpace
NotCF1.  Function Format ( EnumerationType : Type; Value : Object; Formatting : String ) : String; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
The Format method converts the EnumerationType Value into a string representation.
 
The Formatting string 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)
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 formatting 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;

  Result : String;

begin
  // Using a simple enum
  Brian  := Fat;
  Result := System.Enum.Format(TypeOf(Attributes), Brian, 'G');
  Console.WriteLine('Brian is ' + Result);
  Result := System.Enum.Format(TypeOf(Attributes), Brian, 'D');
  Console.WriteLine('Brian is ' + Result);
  Result := System.Enum.Format(TypeOf(Attributes), Brian, 'F');
  Console.WriteLine('Brian is ' + Result);
  Result := System.Enum.Format(TypeOf(Attributes), Brian, 'X');
  Console.WriteLine('Brian is ' + Result);
  Console.WriteLine;

  // Using a simple enum incorrectly
  Paul   := Tall or Fat;
  Result := System.Enum.Format(TypeOf(Attributes), Paul,  'G');
  Console.WriteLine('Paul  is ' + Result);
  Console.WriteLine;

  // Using a flags enum as an enum
  Fred   := Clever;
  Result := System.Enum.Format(TypeOf(Qualities),  Fred,  'G');
  Console.WriteLine('Fred  is ' + Result);
  Console.WriteLine;

  // Using a flags enum as a set
  Jim    := Sexy or Clever;
  Result := System.Enum.Format(TypeOf(Qualities),  Jim,   'G');
  Console.WriteLine('Jim   is ' + Result);

  Console.ReadLine;
end.
   Brian is Fat
   Brian is 2
   Brian is Fat
   Brian is 02
  
   Paul  is 3
  
   Fred  is Clever
  
   Jim   is Sexy, Clever
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author