Home  |  Delphi .net Home  |  System.Int16  |  Parse Method
Parse  
Method  
Converts a string representation of a Int16 into a Int16 value
Int16 Class
System NameSpace
CF1.  Function Parse ( Value : String; ) : Int16;
CF2.  Function Parse ( Value:StringValue : String; Style : NumberStyles; ) : Int16;
CF3.  Function Parse ( Value:StringValue : String; FormatProvider : IFormatProvider; ) : Int16;
CF4.  Function Parse ( Value:StringValue : String; Style : NumberStyles; FormatProvider : IFormatProvider; ) : Int16; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
Attempts to parse the Value string into a value between -32768 and 32767, returning a Int16 object with this value.
 
The Style parameter determines the allowed number content. It is an enumerated type that is treated as a set of flags (it has the [Flags] attribute). This means that multiple values may be set, using logical or. The possible values are :
 
AllowCurrencySymbol Allow for ?,$ ...
AllowExponentE+000 format
AllowThousandsFor example : 1,000,000
AllowDecimalPointFor example : 123.456
AllowParenthesesFor example (1234)
AllowTrailingSignFor example : 123-
AllowLeadingSignFor example : -123
AllowTrailingWhiteAllow trailing blanks
AllowLeadingWhiteAllow leading blanks
AllowHexSpecifierFor example : 0x2bcd

 
Note that not all of these allowances are meaningful for Int16 values.
 
The FormatProvider option allows for customised formatting and is beyond the scope of Delphi Basics.
Notes
Warning : An exception is thrown if the parse encounters unexpected characters.
References
NumberStyles
Microsoft MSDN Links
System
System.Int16
 
 
A simple example
// Full Unit code.
// -------------------------------------------------------------
// Create a new WinForm application, double click the form to
// create an OnLoad event, and then replace the WinForm unit
// with this text.
 
unit WinForm;
 
interface
 
uses
  System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data;
 
type
  TWinForm = class(System.Windows.Forms.Form)
  \{REGION 'Designer Managed Code'\} // Note that REGION and ENREGION should be prefixed by a dollar sign
  strict private
    ///
    /// Required designer variable.
    ///

    Components: System.ComponentModel.Container;
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///

    procedure InitializeComponent;
    procedure TWinForm_Load(sender: System.Object; e: System.EventArgs);
  {ENDREGION}
  strict protected
    ///
    /// Clean up any resources being used.
    ///

    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;
 
  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]
 
implementation
 
\{REGION 'Windows Form Designer generated code'\}
///
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
///

 
 
procedure TWinForm.InitializeComponent;
begin
  //
  // TWinForm
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(292, 266);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Include(Self.Load, Self.TWinForm_Load);
end;
{ENDREGION}
 
procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;
 
constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
 
procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Globalization;

var
  intStr : String;
  result : Integer;

begin
  intStr := '23';
  result := System.Int16.Parse(intStr);
  Console.WriteLine('''' + intStr + ''' parses to {0}', result.ToString);

  Console.ReadLine;
end.
 
end.
Hide full unit code
  '23' parses to 23
Using NumberStyles
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Globalization;

var
  style  : NumberStyles;
  intStr : String;
  result : Integer;

begin
  // Allow for hex values and leading/trailing blanks
  style := NumberStyles.AllowLeadingWhite  or
           NumberStyles.AllowTrailingWhite or
           NumberStyles.AllowHexSpecifier;

  intStr := ' 12AB ';   // Hex 12AB = 4779
  result  := System.Int16.Parse(intStr, style);
  Console.WriteLine('''' + intStr + ''' parses to {0}', result.ToString);

  // Or more simply using one combined number style value
  style := NumberStyles.HexNumber;

  intStr := ' FF00 ';   // Hex FF00 = -256
  result  := System.Int16.Parse(intStr, style);
  Console.WriteLine('''' + intStr + ''' parses to {0}', result.ToString);

  Console.ReadLine;
end.
Show full unit code
  ' 12AB ' parses to 4779
  ' FF00 ' parses to -256
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author