Is it the same to use .AsString as .Text to get the value of a field from a DataSet?


After seeing a certain code, I realized that they use this style:


It seems to work fine, but I had used it so far:


I have used both when loading a TMemo and there seems to be no difference.


Is there any reason why I should use one and not the other? What is that reason?

Normally, for a memo, I use:


And this works well too.

asked by jachguate 30.10.2015 в 00:54

3 answers


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.

  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);

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'


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.

answered by 30.10.2015 / 14:59

.AsString returns the same " string " as the property .Text if there is no event handler "Event Handler", OnGetText , but if one exists, it may be the case being different, or perhaps a descendant in TField other than the standard could cause it to be different.

Greetings and I hope you help.

P.D: As to whether there is a reason to use one or the other, I would not be able to tell you with complete certainty that this would depend on the use you have planned for the return value.

answered by 30.10.2015 в 01:45

About the following fragment of the question:


Normally, for a memo, I use:


And this works well too.

This last practice using the Assign method is completely inappropriate and can lead to errors that are difficult to debug. With the Assign method, the same memory address is being assigned and in this sense if the memory of the History field were free (for the example), the content would also be without value ( nil ) ( Lines ) of the memo.

I recommend:

aMemo.Lines.Text := tblAcct.FieldByName('History').Text;
answered by 18.12.2015 в 20:56