为什么单独的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
仍然是其自己的全局变量(在XE中为deprecated
)在SysUtils
单元中。在XE3中,该变量已删除。您必须使用LongMonthNames
的TFormatSettings
成员,该成员在SysUtils
单元中具有全局变量:
var
// Note: Using the global FormatSettings formatting variables is not thread-safe.
FormatSettings: TFormatSettings;
您不必编写完全限定的路径,只需
FormatSettings.LongMonthNames[x]
即可:Writeln(FormatSettings.LongMonthNames[12]);
如果创建自己的
TFormattSettings
实例,则可以安全地在线程中使用(只要遵守通常的线程安全规则):var
Fmt: TFormatSettings;
begin
Fmt := TFormatSettings.Create;
Writeln(Fmt.LongMonthNames[12]);
end;