Converting Field Values
Go Up to Displaying, Converting, and Accessing Field Values
Conversion properties attempt to convert one data type to another. For example, the AsString property converts numeric and Boolean values to string representations. The following table lists field component conversion properties, and which properties are recommended for field components by field-component class:
Field Components | AsVariant | AsString | AsInteger | AsFloat, AsCurrency, AsBCD | AsDateTime, AsSQLTimeStamp | AsBoolean |
---|---|---|---|---|---|---|
TStringField |
yes |
NA |
yes |
yes |
yes |
yes |
TWideStringField |
yes |
yes |
yes |
yes |
yes |
yes |
TIntegerField |
yes |
yes |
NA |
yes |
||
TSmallIntField |
yes |
yes |
yes |
yes |
||
TWordField |
yes |
yes |
yes |
yes |
||
TLargeintField |
yes |
yes |
yes |
yes |
||
TFloatField |
yes |
yes |
yes |
yes |
||
TCurrencyField |
yes |
yes |
yes |
yes |
||
TBCDField |
yes |
yes |
yes |
yes |
||
TFMTBCDField |
yes |
yes |
yes |
yes |
||
TDateTimeField |
yes |
yes |
yes |
yes |
||
TDateField |
yes |
yes |
yes |
yes |
||
TTimeField |
yes |
yes |
yes |
yes |
||
TSQLTimeStampField |
yes |
yes |
yes |
yes |
||
TBooleanField |
yes |
yes |
||||
TBytesField |
yes |
yes |
||||
TVarBytesField |
yes |
yes |
||||
TBlobField |
yes |
yes |
||||
TMemoField |
yes |
yes |
||||
TGraphicField |
yes |
yes |
||||
TVariantField |
NA |
yes |
yes |
yes |
yes |
yes |
TAggregateField |
yes |
yes |
Note that some columns in the table refer to more than one conversion property (such as AsFloat, AsCurrency, and AsBCD). This is because all field data types that support one of those properties always support the others as well.
Note also that the AsVariant property can translate among all data types. For any datatypes not listed above, AsVariant is also available (and is, in fact, the only option). When in doubt, use AsVariant.
In some cases, conversions are not always possible. For example, AsDateTime can be used to convert a string to a date, time, or datetime format only if the string value is in a recognizable datetime format. A failed conversion attempt raises an exception.
In some other cases, conversion is possible, but the results of the conversion are not always intuitive. For example, what does it mean to convert a Data.DB.TDateTimeField value into a float format? AsFloat converts the date portion of the field to the number of days since 12/31/1899, and it converts the time portion of the field to a fraction of 24 hours. The following table lists permissible conversions that produce special results:
Special conversion results :
Conversion | Result |
---|---|
String to Boolean |
Converts "True," "False," "Yes," and "No" to Boolean. Other values raise exceptions. |
Float to Integer |
Rounds float value to nearest integer value. |
DateTime or SQLTimeStamp to Float |
Converts date to number of days since 12/31/1899, time to a fraction of 24 hours. |
Boolean to String |
Converts any Boolean value to "True" or "False." |
In other cases, conversions are not possible at all. In these cases, attempting a conversion also raises an exception.
Conversion always occurs before an assignment is made. For example, the following statement converts the value of CustomersCustNo to a string and assigns the string to the text of an edit control:
Edit1.Text := CustomersCustNo.AsString;
Edit1->Text = CustomersCustNo->AsString;
Conversely, the next statement assigns the text of an edit control to the CustomersCustNo field as an integer:
MyTableMyField.AsInteger := StrToInt(Edit1.Text);
MyTableMyField->AsInteger = StrToInt(Edit1->Text);