为什么我不能将ResourceString用作常量?

4
我已经从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): 需要常量表达式
[Pascal错误] IDEVirtualResources.pas(156): 需要常量表达式
[Pascal错误] IDEVirtualResources.pas(157): 需要常量表达式

更新
widestring更改为string可以防止编译器报错,(我怀疑这会在其他地方创建一些问题,因为widestring <> string),但我想保持常量的类型为widestring


2
代码可能是针对D2009+编写的,其中资源字符串是WideStrings。 - Uwe Raabe
1
就此而言,D2009和D2010没有抱怨这个问题。可能与Unicode有关。毕竟,WideStrings是Windows管理的UTF-16LE字符串...而在D2009之前的资源字符串是AnsiStrings。 - Marjan Venema
1
如果 AnsiString 可以容纳您需要的字符,则无需担心将其存储为 AnsiString,然后转换为 WideString。编译器会自动透明地为您执行此操作。 - David Heffernan
@Uwe 在 D2009+ 中,resourcestrings 是 System.string,如果相信 IDE 智能提示的话。 - David Heffernan
1
@Uwe 看来不能完全相信 IDE 洞察力! - David Heffernan
显示剩余2条评论
1个回答

4
正如Uwe在评论中指出的那样,Delphi Unicode版本中的“resourcestring”类型是“WideString”。但是您正在使用预Unicode Delphi,因此“resourcestring”只是“AnsiString”。这就解释了编译错误。
如何继续取决于您要尝试做什么。如果您打算将这些字符串翻译成不同的语言,则可能会陷入困境。如果您打算这样做,那么使用Unicode版本的Delphi显然会更好。
因此,由于您坚持使用预Unicode Delphi,我猜您实际上并不需要翻译这些字符串。在这种情况下,只需将“const”数组的声明从“WideString”更改为“string”。碰巧,此代码声明了该数组,但从未引用过它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接