Delphi XE2和Delphi XE7中的LongMonthNames用法

Delphi XE2和Delphi XE7中的LongMonthNames用法,delphi,delphi-xe2,delphi-xe3,delphi-xe7,Delphi,Delphi Xe2,Delphi Xe3,Delphi Xe7,为什么LongMonthNames[X]单独(没有名称空间前缀)在Delphi XE7中不起作用,而在Delphi XE2中却起作用 program LongMonthNames_Test; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils; begin try // Works in both Delphi XE2 and Delphi XE7: Writeln(System.SysUtils.FormatSe

为什么
LongMonthNames[X]
单独(没有名称空间前缀)在Delphi XE7中不起作用,而在Delphi XE2中却起作用

program LongMonthNames_Test;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

begin
  try
    // Works in both Delphi XE2 and Delphi XE7:
    Writeln(System.SysUtils.FormatSettings.LongMonthNames[12]);

    // Works only in Delphi XE2, does NOT work in Delphi XE7:
    // ("not work" obviously means does not compile because of errors in the source code)
    Writeln(LongMonthNames[12]);

    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

在XE2中,
LongMonthNames
仍然是它自己在
SysUtils
单元中的全局变量(在XE中是
弃用的
)。在XE3中,该变量被删除。您必须使用
t格式设置
LongMonthNames
成员,该成员在
SysUtils
单元中有一个全局变量:

var
  // Note: Using the global FormatSettings formatting variables is not thread-safe.
  FormatSettings: TFormatSettings;
您不必编写完全限定的路径,只需
FormatSettings即可。LongMonthNames[x]
将执行以下操作:

Writeln(FormatSettings.LongMonthNames[12]);
如果您创建自己的
t格式设置
实例,则在线程中使用它是安全的(只要您遵守通常的线程安全规则):


那么,如果我在各自的线程中创建一个TFormatSettings实例,它是否会是线程安全的呢?@KenWhite:您的链接不起作用。我想你在找这个:@Remy:是的,第一个是我试图链接的;我直接从IE复制了URL。谢谢。当您在XE2中编译项目时,您没有看到关于使用TFormatsettings的编译器警告吗?永远不要忽视警告,它们的存在是有原因的。。。
var
  Fmt: TFormatSettings;
begin
  Fmt := TFormatSettings.Create;
  Writeln(Fmt.LongMonthNames[12]);
end;