整数値の列挙型がわかっている場合、文字列表現を取得するにはどうすればよいですか?
type
MyEnum = (tmp_one, tmp_two, tmp_three);
const
MyTypeNames: array[tmp_one..tmp_three] of string = ('One', 'Two', 'Three');
整数値の列挙型がわかっている場合、文字列表現を取得するにはどうすればよいですか?
type
MyEnum = (tmp_one, tmp_two, tmp_three);
const
MyTypeNames: array[tmp_one..tmp_three] of string = ('One', 'Two', 'Three');
この列挙型の変数ではなく、序数値を持っていると想定しています。その場合は、序数を列挙型にキャストするだけです。このような:
function GetNameFromOrdinal(Ordinal: Integer): string;
begin
Result := MyTypeNames[MyEnum(Ordinal)];
end;
文字列の配列で名前を使用したいと思います。次に、これは非常に簡単です。
var
myEnumVar: MyEnum;
begin
myEnumVar := tmp_two; // For example
ShowMessage(MyTypeNames[myEnumVar]);
列挙型の序数値を使用する必要はありません。列挙型を「添え字」として配列を宣言し、列挙変数を直接使用できます。
type
TMyEnum = (tmp_one, tmp_two, tmp_three);
const
MyTypeNames: array[TMyEnum] of string = ('One', 'Two', 'Three');
function Enum2Text(aEnum: TMyEnum): string;
begin
Result := MyTypeNames[aEnum];
end;
列挙型または列挙型にキャストされた整数値で呼び出します。
Enum2Text(tmp_one); // -> 'One'
Enum2Text(TMyEnum(2)); // -> 'Three'