Integer Type Helpers (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example is a simple Delphi console application that shows how to use the new helper methods for integer types.

Code

uses
  System.SysUtils;

var
  // Declare two of the basic intrinsic types that have helpers.
  // The others are: ShortInt, SmallInt, Word, Cardinal, NativeInt, NativeUInt and Int64.
  // The methods used in this code example apply to all the types mentioned above.
  myByte: Byte;
  myInteger: Integer;

const
  myString: String = '12345678910111211314';
begin
  myByte := myByte.MaxValue; //255
  myInteger := myInteger.MaxValue;  //2147483647

  writeln('Byte value: ', myByte);
  writeln('Byte value as hexadecimal string using four digits: ',
    myByte.ToHexString(Integer(4)));
  writeln('Byte value as string: ', myByte.ToString());

  // Tries to convert myString to an Integer.
  if myInteger.TryParse(myString, myInteger) then
  begin
    writeln('The new value of myInteger is:', myInteger);
  end
  else
  begin
  //In case the conversion does not succeed,
  //the next statement will try to convert the myByte number
  //to a string.
  //The string returned will be converted to an integer value
  //with the Parse function.
    myInteger := myInteger.Parse(myByte.ToString());
    writeln('The new value of myInteger is:', myInteger);
  end;
  readln;

end.

Uses

See Also