RAD Studio |  |

Converting Field Values
See Also
Collapse All

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 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:

Copy Code

    Edit1.Text := CustomersCustNo.AsString;

Copy Code

    Edit1->Text = CustomersCustNo->AsString;

Conversely, the next statement assigns the text of an edit control to the CustomersCustNo field as an integer:

Copy Code

    MyTableMyField.AsInteger := StrToInt(Edit1.Text);

Copy Code

    MyTableMyField->AsInteger = StrToInt(Edit1->Text);

See Also

Displaying Field Component Values in Standard Controls

Accessing Field Values with a Dataset's Fields Property

Accessing Field Values with a Dataset's FieldByName Method

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!