Delphi XE2和Delphi XE7中的LongMonthNames用法

use*_*348 1 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.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.
Run Code Online (Sandbox Code Playgroud)

Dal*_*kar 6

在XE2中,单元LongMonthNames中仍然是它自己的全局变量(deprecated在XE中)SysUtils.在XE3中,该变量已被删除.您必须使用在单元中具有全局变量的LongMonthNames成员:TFormatSettingsSysUtils

var
  // Note: Using the global FormatSettings formatting variables is not thread-safe.
  FormatSettings: TFormatSettings;
Run Code Online (Sandbox Code Playgroud)

您不必编写完全限定的路径,只需FormatSettings.LongMonthNames[x]执行以下操作:

Writeln(FormatSettings.LongMonthNames[12]);
Run Code Online (Sandbox Code Playgroud)

如果您创建自己的实例TFormattSettings,则可以安全地在线程中使用(只要遵守通常的线程安全规则):

var
  Fmt: TFormatSettings;
begin
  Fmt := TFormatSettings.Create;
  Writeln(Fmt.LongMonthNames[12]);
end;
Run Code Online (Sandbox Code Playgroud)