gpt4 book ai didi

delphi - Delphi XE2 和 Delphi XE7 中 LongMonthNames 的用法

转载 作者:行者123 更新时间:2023-12-02 02:57:43 24 4
gpt4 key购买 nike

为什么 LongMonthNames[X] 单独(没有 namespace 前缀)在 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 中,该变量被删除。您必须使用 TFormatSettingsLongMonthNames 成员,它在 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;

关于delphi - Delphi XE2 和 Delphi XE7 中 LongMonthNames 的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27358545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com