Before answering the answer directly, we will discuss the relevant properties of TField and its descendants.
Text
/ DisplayText
Both properties are used to obtain a textual representation of the value of the field for use in the User Interface.
The purpose of the Text
property is to obtain a textual representation of the field when it is being edited in a data associated control (Data Aware), in contrast to the DisplayText
property that provides the value of the field with display format for the user, for example, it may contain punctuation characters or other decoration at the flat value. (1234.50 in contrast to 1,234.50).
In other words, a TdbEdit
or similar controls will show the value of DisplayText
when they are not being edited, and upon receiving the focus, they will change the value of the property Text
and it is this value that the user "edits" A TdbLabel only pulls the property DisplayText
, since it is not editable.
The documentation for Text
says:
Contains the string to display in a data-aware control when the field is in edit mode
A typical example is a TFloatField
with property Currency
set to True
. The DisplayText property returns the string with the number containing thousands separators, the decimal separator and the currency symbol. The Text property returns a string that does not contain thousands separators or currency symbols.
begin
MyFloatField.Currency := True;
MyFloatField.AsFloat := 1234.56;
A := MyFloatField.Text; //'1234.56'
B := MyFloatField.DisplayText; //'$1,234.56', depends on your locale
ShowMessage(A + #13 + B);
end;
The behavior of both properties can be customized by writing an event handler for OnGetText
, where the logic can be written to convert the value of the field into one of both textual representations. The Boolean parameter DisplayText
indicates whether the requested string is to represent the value in the user interface or for editing.
In this way, we can achieve that the representation in the user interface for an entire field with value 1 is 'one', and its value for editing is 'one'
AsString
On the other hand, the AsString property uses a flat conversion between the base data type of the field and a string of characters. Each descendant of TField
implements virtual method GetAsString
that typically uses functions of the RTL to perform the conversion. Following the TFloatField example, this class calls the FloatToStr () function to perform the conversion.
The answer
All this said, the answer to the question would be:
AsString usually returns the same value that the Text property returns, as long as there is no event handler for OnGetText, but the representation might be different if there is an event handler or a non-standard descendant of TField.
With the information given, it can not be said which is more appropriate, since this depends on what use will be given to the returned value. If it is being used to display values in the User Interface, as shown in the code examples provided, my advice is to use the DisplayText
property.