RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.Dec Function

Decrements a variable by 1 or N.

Pascal
procedure Dec(var X); overload;
procedure Dec(var X; N: Longint); overload;
C++
Dec( X);
Dec( X, Longint N);

System

In Delphi code, Dec subtracts one or N from a variable. 

X is a variable of an ordinal type (including Int64), or a pointer type if the extended syntax is enabled. 

N is an integer-type expression.  

X decrements by 1, or by N if N is specified; that is, Dec(X) corresponds to the statement X := X - 1, and Dec(X, N) corresponds to the statement X := X - N. On some platforms, Dec may generate optimized code, especially useful in tight loops. 

If X is a pointer type, it decrements X by N times the size of the type pointed to. Thus, given

type
  PMyType = ^TMyType;

and

var
  P: PMyType;

the statement Dec(P) decrements P by SizeOf(TMyType).

Warning: Do not use Dec on ordinal-type properties if the property uses a write procedure.
 

Delphi Examples: 

{
This example displays the Cosine of any value between 0 and
12.  Since the scrollbar position is an integer between the
scrollbar  min and max, multiply the range and divide the
position to fine tune it.  With the scrollbar selected use
the left and right buttons to change the position by 1E-6.
Or use the Dec function to decrement (or increment) by a
specific amount.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  new : Integer;
begin
  new := ScrollBar1.Position;
  System.Dec(new, Round(StrToFloat(Edit3.Text)*1000000));
  ScrollBar1.Position := new;
  Edit1.Text := FloatToStr(ScrollBar1.Position/1000000);
  Edit2.Text := FloatToStr(Cos(ScrollBar1.Position/1000000));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ScrollBar1.Max := Round(Pi * 4000000);   // 4 * Pi
end;

procedure TForm1.ScrollBar1Scroll(Sender: TObject; ScrollCode: TScrollCode;
  var ScrollPos: Integer);
begin
  Edit1.Text := FloatToStr(ScrollPos/1000000);
  Edit2.Text := FloatToStr(Cos(ScrollPos/1000000));
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!