RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TField.AsInteger Property

Represents the field's value as a 32-bit integer.

Pascal
property AsInteger: Longint;
C++
__property Longint AsInteger;

Use AsInteger to read the value of the field's data into an integer, or to assign an integer value to the contents of the field. TField does not support integer values, and raises an exception when an attempt is made to get or set the AsInteger property. 

Descendants of TField that represent integer fields or that support conversions between the field's Value property and an integer value, override AsInteger to read and write the value of the field as an integer value.  

Delphi Examples: 

 

{
This example uses the BeforeInsert event to do data 
validation. If the StrToInt function raises an exception, 
the edit control’s contents are set to a valid value so 
that the assignment to the INTEGER field in the dataset 
will succeed.  Continuing after the exception causes a
'0' to be placed in the PORTA field.
DataSet design:
This example requires a ClientDataSet, a DBGrid, a
DataSource and a DBNavigator.  Name the ClientDataSet CDS.
Set the DataSet property in the DataSource to CDS and name
that DataSource DS.  Set the DataSource in the DBNavigator
to DS the DataSource in the DBGrid to DS.  Right click on CDS
and select the Fields Editor, then right click on the Field
Editor form and create two fields by selecting "new field".
In the field configuration window, set the name to HOST, the
Component value will automatically be set to CDSHOST.  Set
the filed type to String.  In the field configuration window
for the second field, set the name to PORTA, the Component
value will automatically be set to CDSPORTA.  Set the field
type to Integer.  The file used to load CDS must have an
attribute name of HOST with an attribute type of string and
an attribute name of PORTA with an attribute type of integer.
Add DB, DBCtrls, Grids, DBGrids and DBClient to the uses clause.
}
procedure TForm1.Button3Click(Sender: TObject);
begin
  CDS.Insert;
  CDS.FieldByName('PORTA').AsInteger := StrToInt(Edit1.Text);
  CDS.Post;
end;

procedure TForm1.CDSBeforeInsert(DataSet: TDataSet);
begin
  try
  { Make sure edit field can be converted to integer --
   this will raise an exception if it can’t }
    StrToInt(Edit1.Text);
  except
    Edit1.Text := '0';
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CDS.LoadFromFile(gsAppPath + 'CDS.XML');
end;

 

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