【问题标题】:Why can't I use a resourcestring as a constant?为什么我不能使用资源字符串作为常量?
【发布时间】:2011-12-10 14:41:53
【问题描述】:

我从http://embtvstools.svn.sourceforge.net/ 下载了embtvstools (Embarcadero TVirtualShellTools)

但是,当我创建一个新包时,删除 .pas 文件 (以及 VirtualTreeView 中缺少的 compilers.inc 并编译该批次,我收到错误 E2026,为什么这个以及如何避免/解决这个问题?

resourcestring
    sAssociationChanged = 'Association Changed';
    sItemCreate = 'Item Create';
    sItemDelete = 'Item Delete';
    ....

const
  // Literal translations of TShellNotifyEvent type.  Useful when using the
  // OnShellNotify event to print out what event occurred.  VirtualShellUtilities.pas
  // has a helper function ShellNotifyEventToStr that uses these.
  VET_NOTIFY_EVENTS: array[0..19] of WideString = (
    sAssociationChanged,
    sAttributes,
    sItemCreate,
    .....

[Pascal 错误] IDEVirtualResources.pas(155): E2026 常量表达式预期
[Pascal 错误] IDEVirtualResources.pas(156): E2026 常量表达式预期
[Pascal 错误] IDEVirtualResources.pas(157): E2026 Constant expression expected

更新
widestring 更改为string 会阻止编译器抱怨,(我怀疑它会在其他地方产生一些问题,因为宽字符串 字符串)我想保留widestring 类型的常量。

【问题讨论】:

  • 该代码可能适用于 D2009+,其中资源字符串是 WideStrings。
  • 对于它的价值,D2009 和 D2010 不会抱怨这一点。可能与Unicode有关。 WideStrings 毕竟是 Windows 管理的 UTF-16LE 字符串......而 D2009 之前的资源字符串是 AnsiStrings。
  • 我不得不说,你甚至可以有一个类型化的常量WideString,这让我感到很惊讶。它们是在运行时使用 COM 分配器分配的真正的 WideStrings,还是只是指向以 null 结尾的 WideChar 数组的指针?
  • 如果 AnsiString 可以保存您需要的字符,那么您无需担心存储为 AnsiString 然后转换为 WideString。编译器会为你透明地做这件事。
  • @Uwe 看来IDE洞察力不靠谱!

标签: delphi delphi-2007


【解决方案1】:

正如 Uwe 在 cmets 中指出的那样,Unicode 版本的 Delphi 中的 resourcestringWideString 类型。但是您使用的是 pre-Unicode Delphi,所以 resourcestring 就是 AnsiString。这解释了编译错误。

如何进行取决于您尝试执行的操作。如果您打算将这些字符串翻译成不同的语言,那么您可能会陷入困境。如果您打算这样做,那么使用 Unicode 版本的 Delphi 显然会好得多。

因此,由于您坚持使用 Unicode 之前的 Delphi,我猜您实际上并不需要翻译字符串。在这种情况下,只需将 const 数组的声明从 WideString 更改为 string。碰巧的是,这个数组是由这段代码声明的,但从未被引用过。

【讨论】:

    猜你喜欢
    • 2011-07-02
    • 2012-06-03
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多