是否可以在 Delphi 和 FreePascal 中声明数组的 const 而不让元素为常量?

发布于 2024-07-17 06:06:30 字数 386 浏览 3 评论 0原文

很久以前,我记得我可以在 Turbo Pascal 7 中做到这一点。

也许我错了,这是我需要澄清的事情,但是是否可以将字符串数组声明为常量?

如果不是,有什么选项/解决方法。

我现在拥有的是:

type
  TStates = (sOne, sTwo, sThree);
var
  TArrayOfString: array [sOne..sThree] of string = 
     ('State one', 'State two', 'State three');

但想用 const 替换该 var。

谢谢

编辑1:添加了更多代码来澄清我的问题。

A long time ago I remember I could do this in Turbo Pascal 7.

Maybe I'm wrong and it's something I need to clarify, but is it possible to declare an array of strings as a constant?

If not what's the option/workaround.

What I have now is:

type
  TStates = (sOne, sTwo, sThree);
var
  TArrayOfString: array [sOne..sThree] of string = 
     ('State one', 'State two', 'State three');

but would want to replace that var with a const.

Thanks

Edit 1: Added some more code to clarify my question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み零 2024-07-24 06:06:30

只需将 var 替换为 const 是完全合法的:

const
  TArrayOfString: array [1..3] of string =
     ('String one', 'String two', 'String three');

我很好奇为什么你的标识符名称以 T 开头。 您是否尝试定义这样的类型:

type
  TArrayOfString = array [1..3] of string;
const
  MyArrayOfString: TArrayOfString =
     ('String one', 'String two', 'String three');

您不能将可变长度数组(AFAIK)作为 const,也不能将其设为未定义的类型。

这是使用 Delphi 2009。YMMV 和 FreePascal。

Just replacing var with const is perfectly legal:

const
  TArrayOfString: array [1..3] of string =
     ('String one', 'String two', 'String three');

I am curious why your identifier name starts with a T though. Were you trying to define a type like this:

type
  TArrayOfString = array [1..3] of string;
const
  MyArrayOfString: TArrayOfString =
     ('String one', 'String two', 'String three');

You cannot have a variable length array (AFAIK) as a const, nor can you have it of an undefined type.

This is with Delphi 2009. YMMV with FreePascal.

把回忆走一遍 2024-07-24 06:06:30

在过去的 pascal/delphi 中,当您写道:

const 
  A : Integer = 5;

您没有定义常量,而是定义了初始化变量。

您可以毫无问题地定义:

const
  A : array [1..2] of string = ('a', 'b');

但字符串也必须是常量。 它们需要在编译时就知道。

同样的情况也适用:

var
  A : array [1..2] of string = ('a', 'b');

所以你不能写:

var
  B : string = 'hi';
  A : array [1..2] of string = (B, 'b');

因为 B 是一个 var。 但你可以这样写:

const
  B = 'hi'; // Even a typed constant does not work.

var
  A : array [1..2] of string = (B, 'b');

请注意,提供了选项:“可分配类型常量”(默认为 false)来创建可以分配的旧时间类型常量。 它只是为了向后兼容,因为您确实希望常量保持不变。

In old day pascal/delphi when you wrote:

const 
  A : Integer = 5;

You did not define a constant, but an initialized variable.

You can define without problem:

const
  A : array [1..2] of string = ('a', 'b');

But the strings have to be constants too. They need to be known at compile time.

The same goes for:

var
  A : array [1..2] of string = ('a', 'b');

So you can't write:

var
  B : string = 'hi';
  A : array [1..2] of string = (B, 'b');

Because B is a var. But you can write:

const
  B = 'hi'; // Even a typed constant does not work.

var
  A : array [1..2] of string = (B, 'b');

Note that the option: "Assignable typed constants" (default false) is provided to create the old time typed constants that can be assigned. It is just there for backwards compatibility, because you really want your constants to be constant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文