1. Computing & Technology
Delphi for .NET FCL Examples Reference
Convert namespace
System
Declaration
Convert = class sealed(System.Object);
Description
Converts a base data type to another base data type. Convert is a sealed class.
Example
The following sample demonstrates some of the conversion methods in this class, including ToInt32, ToBoolean, and ToString.
program System_Convert;

{$APPTYPE CONSOLE}
{$AUTOBOX ON}

type
  SamplesConvert = class
  public
    class procedure Main;
    class procedure Main2;
  end;

{ SamplesConvert }

class procedure SamplesConvert.Main;
var
  xSbyte: System.SByte;
  xUlong: System.UInt64;
  xUint: Cardinal;
  xUshort: System.UInt16;
  xByte: Byte;
  xChar: WideChar;
  xString: string;
  xDecimal: System.Decimal;
  xDouble: System.Double;
  xSingle: System.Single;
  xLong: Int64;
  xInt: Integer;
  xShort: SmallInt;
  xBool: Boolean;
  str: string;
  nl: string;
begin
  nl := Environment.NewLine;
  str := '{0}Return the Int64 equivalent of
           the following base types:{0}';
  xBool := False;
  xShort := 1;
  xInt := 2;
  xLong := 3;
  xSingle := 4;
  xDouble := 5;
  xDecimal := 6.0;
  xString := '7';
  xChar := '8'; // '8' = hexadecimal 38 = decimal 56
  xByte := 9;
  xUshort := 120;
  xUint := 121;
  xUlong := 122;
  xSbyte := 123;
  with Console do
  begin
    WriteLine(str, nl);
    WriteLine('Boolean:  {0}', Convert.ToInt64(xBool));
    WriteLine('Int16:    {0}', Convert.ToInt64(xShort));
    WriteLine('Int32:    {0}', Convert.ToInt64(xInt));
    WriteLine('Int64:    {0}', Convert.ToInt64(xLong));
    WriteLine('Single:   {0}', Convert.ToInt64(xSingle));
    WriteLine('Double:   {0}', Convert.ToInt64(xDouble));
    WriteLine('Decimal:  {0}', Convert.ToInt64(xDecimal));
    WriteLine('String:   {0}', Convert.ToInt64(xString));
    WriteLine('Char:     {0}', Convert.ToInt64(xChar));
    WriteLine('Byte:     {0}', Convert.ToInt64(xByte));
    WriteLine('DateTime: There is no example of this');
    Write('          conversion because a DateTime');
    Write('          cannot be converted to an Int64.');
    Write('{0}The following types', nl);
    WriteLine('are not CLS-compliant.{0}', nl);
    WriteLine('UInt16:   {0}', Convert.ToInt64(xUshort));
    WriteLine('UInt32:   {0}', Convert.ToInt64(xUint));
    WriteLine('UInt64:   {0}', Convert.ToInt64(xUlong));
    WriteLine('SByte:    {0}', Convert.ToInt64(xSbyte));
  end;
end;

class procedure SamplesConvert.Main2;
const
  dNumber = 23.15;
var
  newInteger: Integer;
  chrNumber: WideChar;
  strNumber: System.String;
  bNumber: Boolean;
  iNumber: Integer;
begin
  try
    // Returns 23
    iNumber := System.Convert.ToInt32(dNumber);
  except
    on System.OverflowException do
      System.Console.WriteLine
      ('Overflow in double to int conversion.');
  end;

  // Returns True
  bNumber := System.Convert.ToBoolean(dNumber);

  // Returns "23.15"
  strNumber := System.Convert.ToString(dNumber);

  try
    // Returns '2'
    chrNumber := System.Convert.ToChar(strNumber[1]);
  except
    on System.ArgumentNullException do
      System.Console.WriteLine
      ('String is null');
    on System.FormatException do
      System.Console.WriteLine
      ('String length is greater than 1.');
  end;

  // System.Console.ReadLine() returns a string
  // and it must be converted.
  newInteger := 0;
  try
    System.Console.WriteLine('Enter an integer:');
    newInteger := System.Convert.ToInt32
                  (System.Console.ReadLine);
  except
    on System.ArgumentNullException do
      System.Console.WriteLine('String is null.');
    on System.FormatException do
      System.Console.WriteLine
      ('String does not consist of an optional 
        sign followed by a series of digits.');
    on System.OverflowException do
      System.Console.WriteLine
      ('Overflow in string to int conversion.');
  end;
  
  System.Console.WriteLine
         ('Your integer as a double is {0}', 
           System.Convert.ToDouble(newInteger));
end;

//test program
begin
  SamplesConvert.Main;
  SamplesConvert.Main2;
end.
Example output
Return the Int64 equivalent of the following base types:

Boolean:  0
Int16:    1
Int32:    2
Int64:    3
Single:   4
Double:   5
Decimal:  6
String:   7
Char:     56
Byte:     9
DateTime: There is no example of this conversion because
          a DateTime cannot be converted to an Int64.

The following types are not CLS-compliant.

UInt16:   120
UInt32:   121
UInt64:   122
SByte:    123

//-----------------------------

Enter an integer: (2005)
Your integer as a double is 2005

Looking for Delphi for .NET demos / source code samples? Look no further, Delphi for .NET FCL Examples Reference is what you are searching for!

Discuss in my forum

©2012 About.com. All rights reserved.

A part of The New York Times Company.