是否可以声明一个包含另一个常量数组的常量数组?

时间:2023-01-13 19:29:13

I want to do something like this:

我想做这样的事情:

const

  MyFirstConstArray: array[0..1] of string = ('Hi', 'Foo');
  MySecondConstArrayWhichIncludesTheFirstOne: array[0..2] of string = 
    MyFirstConstArray + ('Bar');

Basically I want the following result:

基本上我想要以下结果:

MyFirstConstArray -> ('Hi', 'Foo');
MySecondConstArrayWhichIncludesTheFirstOne -> ('Hi', 'Foo', 'Bar');

Is it possible somehow?

有可能吗?

4 个解决方案

#1


2  

AFAIK, you can't do that.
But if the goal is to ensure you declare your actual constant string only once, I suggest you declare the individual strings and then group them in arrays:

AFAIK,你做不到。但是如果目标是确保只声明一次实际的常量字符串,我建议您声明各个字符串,然后将它们分组在数组中:

const
  MyConst1 = 'Hi';
  MyConst2 = 'Foo';
  MyConst3 = 'Bar';
  MyFirstConstArray: array[0..1] of string = (MyConst1, MyConst2);
  MySecondConstArrayWhichIncludesTheFirstOne: array[0..2] of string = 
    (MyConst1, MyConst2, MyConst3);

BTW, your syntax is incorrect, you have to precise the type of the array elements.

顺便说一句,你的语法不正确,你必须精确的数组元素的类型。

#2


1  

If order isn't relevant, then use enumerated sets.

如果订单不相关,则使用枚举集。

type
  TMyConsts = (tConstFoo, tConstHi, TConstBar);
const
  MyFirstConstSet = [tConstFoo, tConstHi];
  MySecondConstSet = MyFirstConstSet + [TConstBar];
  MyConstStrings: array[TMyConsts] of string = ('Foo', 'Hi', 'Bar');

You can use the MyConstStrings array to resolve your enumerations into strings if you want. Depends on your objective.

如果需要,可以使用MyConstStrings数组将枚举解析为字符串。取决于你的目标。

#3


1  

Actually, you can but do it using records. i use this technique in a big way for creating definitions for certain behaviours of entities in my software. it's a very powerful technique:

实际上,你可以使用记录来做。我在很大程度上使用这种技术来为我的软件中的某些实体行为创建定义。这是一项非常强大的技术:

type
      TPerson=record
        // generally you'd put all kinds of addition stuff here including enums, 
        // sets, etc
        saPets:array[0..2] of string;
      end;

const
  scDog='Dog';

  MyPeople:array[0..1] of TPerson=
    ((saPets:(scDog, 'Cat', 'Fish'); ),
     (saPets:('Iguana', 'Tarantula', ''); ));

begin
  ShowMessage(MyPeople[0].saPets[0]);

end;

one thing you can't do with it is refer to resource strings. therefore any translation must be done by the code that retrieves the value. i generally index them with enums--in doing so, i can make the software easier to change because it won't compile if i've left something important out.

你不能用它做的一件事是引用资源字符串。因此,任何转换必须由检索值的代码完成。我通常用枚举索引它们 - 这样做,我可以使软件更容易更改,因为如果我留下重要的东西它将无法编译。

#4


0  

I don't think so. You'll have to do it in code. If these are global constants, you can do the initialization in the 'initialization' section of the unit.

我不这么认为。你必须在代码中做到这一点。如果这些是全局常量,则可以在单元的“初始化”部分进行初始化。

#1


2  

AFAIK, you can't do that.
But if the goal is to ensure you declare your actual constant string only once, I suggest you declare the individual strings and then group them in arrays:

AFAIK,你做不到。但是如果目标是确保只声明一次实际的常量字符串,我建议您声明各个字符串,然后将它们分组在数组中:

const
  MyConst1 = 'Hi';
  MyConst2 = 'Foo';
  MyConst3 = 'Bar';
  MyFirstConstArray: array[0..1] of string = (MyConst1, MyConst2);
  MySecondConstArrayWhichIncludesTheFirstOne: array[0..2] of string = 
    (MyConst1, MyConst2, MyConst3);

BTW, your syntax is incorrect, you have to precise the type of the array elements.

顺便说一句,你的语法不正确,你必须精确的数组元素的类型。

#2


1  

If order isn't relevant, then use enumerated sets.

如果订单不相关,则使用枚举集。

type
  TMyConsts = (tConstFoo, tConstHi, TConstBar);
const
  MyFirstConstSet = [tConstFoo, tConstHi];
  MySecondConstSet = MyFirstConstSet + [TConstBar];
  MyConstStrings: array[TMyConsts] of string = ('Foo', 'Hi', 'Bar');

You can use the MyConstStrings array to resolve your enumerations into strings if you want. Depends on your objective.

如果需要,可以使用MyConstStrings数组将枚举解析为字符串。取决于你的目标。

#3


1  

Actually, you can but do it using records. i use this technique in a big way for creating definitions for certain behaviours of entities in my software. it's a very powerful technique:

实际上,你可以使用记录来做。我在很大程度上使用这种技术来为我的软件中的某些实体行为创建定义。这是一项非常强大的技术:

type
      TPerson=record
        // generally you'd put all kinds of addition stuff here including enums, 
        // sets, etc
        saPets:array[0..2] of string;
      end;

const
  scDog='Dog';

  MyPeople:array[0..1] of TPerson=
    ((saPets:(scDog, 'Cat', 'Fish'); ),
     (saPets:('Iguana', 'Tarantula', ''); ));

begin
  ShowMessage(MyPeople[0].saPets[0]);

end;

one thing you can't do with it is refer to resource strings. therefore any translation must be done by the code that retrieves the value. i generally index them with enums--in doing so, i can make the software easier to change because it won't compile if i've left something important out.

你不能用它做的一件事是引用资源字符串。因此,任何转换必须由检索值的代码完成。我通常用枚举索引它们 - 这样做,我可以使软件更容易更改,因为如果我留下重要的东西它将无法编译。

#4


0  

I don't think so. You'll have to do it in code. If these are global constants, you can do the initialization in the 'initialization' section of the unit.

我不这么认为。你必须在代码中做到这一点。如果这些是全局常量,则可以在单元的“初始化”部分进行初始化。