有 TCaptions 的 StringHelper 吗?

Mar*_*lot 2 delphi delphi-10.4-sydney

我可以添加一个单元来提供 StringHelper 吗TCaption?

使用Edit1.Text.Trim,等比,等Edit1.Text.ToInteger简洁得多。Trim(Edit1.Text)StrToInt(Edit1.Text)

Rem*_*eau 5

没有 的类助手TCaption,不。

TCaptionstring与用于 DFM 目的的其自己的 RTTI是不同的类型,因此该标准TStringHelper不会将其识别为string.

我希望类型别名TCaption能够访问其基类型的帮助器,但可惜,事实并非如此。QualityPortal 中已经有此问题的报告:RSP-13736、RSP-16486、RSP-36474等。

与此同时,你能做的最好的就是将 转换TCaption为 a string,例如:

string(Edit1.Text).Trim
string(Edit1.Text).ToInteger
...
Run Code Online (Sandbox Code Playgroud)

否则,您将必须编写自己的类助手,将TCaption其工作委托给标准string助手。


And*_*and 5

不幸的是没有。

TCaption定义为

type
  TCaption = type string;
Run Code Online (Sandbox Code Playgroud)

string这使得它与(​​因为第二个)不同,type因此TStringHelper不会用于TCaption。并且没有TCaptionHelper = record helper for TCaption(理想情况下与 相同TStringHelper)。

在这种情况下,这非常烦人,但是语言的这种行为使我可以定义自己的语言TShoeSize = type Integer并添加自己的语言,TShoeSizeHelper = record helper for TShoeSize而不会丢失Integer助手。

我个人在 Embarcadero Jira 中建议了一项功能,可以让您复制或继承记录助手;在这种情况下,您可以使用这样的功能来告诉 DelphiTCaption应该具有与string.

但在语言改变之前——或者有人只是复制粘贴TStringHelper到TCaptionHelper——你能做的最好的事情就是:

ShowMessage(string(Edit1.Text).ToUpper)
Run Code Online (Sandbox Code Playgroud)

但请注意,这很危险!

如果不小心写了怎么办

ShowMessage(string(Edit1).ToUpper)
Run Code Online (Sandbox Code Playgroud)

这将编译(指针就是指针......)。