使用.AsString还是.Text? [英] Using .AsString or .Text?

查看:106
本文介绍了使用.AsString还是.Text?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在他们使用的地方看到了一些代码(D5)...

I have just seen a bit of code (D5) where they used...

aStr:=tblAcct.FieldByName('Name').Text;

似乎工作正常,但我一直都使用...

It seems to work fine but I have always used...

aStr:=tblAcct.FieldByName('Name').AsString;

我在加载TMemo时都用过,似乎也没有什么区别。

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

aMemo.Lines.Text:=tblAcct.FieldByName('History').Text;
aMemo.Lines.Text:=tblAcct.FieldByName('History').AsString;

我是否有理由要一个使用另一个?
如果是这样,哪个?

Is there a reason why I should use one over the other? If so, which one?

实际上是TMemo,我通常使用...

Actually for TMemo, I usually use...

aMemo.Lines.Assign(tblAcct.FieldByName('History'))

这似乎也很好。

谢谢

推荐答案

Text 属性用于获取DataAware控件中正在编辑的字段的文本表示形式。 /Data.DB.TField.DisplayText rel = noreferrer> DisplayText 属性,该属性为您提供了一个字符串来向用户表示值(它可能包含标点或其他修饰)。


包含要在数据感知控件中显示的字符串当字段处于编辑模式时

Contains the string to display in a data-aware control when the field is in edit mode

一个典型示例是一个TFloatField,其 Currency 属性设置为 True DisplayText 为您提供了一个字符串,其中包含逗号(如果需要),小数点分隔符和货币符号。 Text 属性为您提供一个不含逗号或货币符号的字符串。

A typical example is a TFloatField with the Currency property set to True. The DisplayText gives you a string with the number containing commas (if needed), the decimal separator and a currency symbol. The Text property gives you a string without commas or currency symbol.

begin
  MyFloatField.Currency := True;
  MyFloatField.AsFloat := 1234.56;
  A := MyFloatField.Text; //'1234.56'
  B := MyFloatField.DisplayText; //'$1,234.56', depends on your locale
end;

以上两个属性都可以通过编写 OnGetText 事件处理程序,您可以在其中编写自定义逻辑到值转换为字符串表示形式。 DisplayText 参数指示所需的字符串是否表示要编辑的值。

Both above properties can be customized writing a OnGetText event handler where you can write custom logic to convert the value to a string representation. The DisplayText parameter indicates if the wanted string is meant to represent the value for edit or not.

另一方面, AsString 属性在基本数据类型和字符串之间使用更简单的转换。每个TField子代都使用RTL中的函数实现虚拟GetAsString方法来执行该表示。在TFloatField示例之后,此类调用 FloatToStr()

On the other hand, the AsString property uses a more plain conversion between the base data type and string. Each TField descendant implements the virtual GetAsString method using functions from the RTL to perform that representation. Following the TFloatField example, this class calls FloatToStr() for that purpose.

这一切,对您问题的答案是:<$如果没有OnGetText事件处理程序,c $ c> AsString 返回与 Text 属性相同的字符串,但是如果有事件处理程序,则返回的字符串可能会有所不同

All this said, the answer to your question is: AsString returns the same string as the Text property if there's no OnGetText event handler, but it may be different if there's a event handler or a non-standard TField descendant.

我无法告诉您哪个更适合您,因为这取决于返回值的预期用途,但是如果您正在使用它在UI中向用户显示值(如您的代码示例),我建议您使用DisplayText属性。

I can't tell what is more appropriate for you, because it depends on what's the intended use for the returned value, but if you're using it to display the values to the user in the UI (as your code example), I advise you to use the DisplayText property.

这篇关于使用.AsString还是.Text?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆