将整数值转换为枚举类型 [英] Convert Integer value to Enumeration Type
问题描述
我可以确定 delphi 将升序的整数分配给我创建的所有枚举吗?
Can I be sure that delphi assign integer number in a ascending sequence to any enumeration a my create ?
type
TMyType = (mtFirst, mtSecond, mtThird);
可以确定mtFirts始终为 TmyType(1)
???
there for sure mtFirts is always TmyType(1)
???
我尝试编写类似 myTypeselection的代码:= TMyType(MyRadiogroup.ItemIndex + 1);
但是我无法通过整数混合数值。
But I fail that the values Integer numbers are somehow mixed .
推荐答案
一个href = http://docwiki.embarcadero.com/RADStudio/zh/Simple_Types#Enumerated_Types>文档具有答案:
The documentation has the answer:
默认情况下,枚举值的序数从0开始,并遵循其标识符在类型声明中列出的顺序。
By default, the ordinalities of enumerated values start from 0 and follow the sequence in which their identifiers are listed in the type declaration.
因此,您认为编号从一开始的想法实际上是错误的。就是 ord(mtFirst)= 0
, ord(mtSecond)= 1
的情况,依此类推。
So your belief that numbering starts from one is in fact incorrect. It is the case the ord(mtFirst)=0
, ord(mtSecond)=1
and so on.
这意味着您的代码应读取:
Which means your code should read:
myTypeselection := TMyType(MyRadiogroup.ItemIndex);
因为无线电组索引也是基于零的。
Because radio group indexing is also zero based.
在我自己的代码中,我使用以下泛型类执行以下操作:
In my own code, I use the following generic class to perform operations like this:
type
TEnumeration<T> = class
strict private
class function TypeInfo: PTypeInfo; inline; static;
class function TypeData: PTypeData; inline; static;
public
class function IsEnumeration: Boolean; static;
class function ToOrdinal(Enum: T): Integer; inline; static;
class function FromOrdinal(Value: Integer): T; inline; static;
class function MinValue: Integer; inline; static;
class function MaxValue: Integer; inline; static;
class function InRange(Value: Integer): Boolean; inline; static;
class function EnsureRange(Value: Integer): Integer; inline; static;
end;
class function TEnumeration<T>.TypeInfo: PTypeInfo;
begin
Result := System.TypeInfo(T);
end;
class function TEnumeration<T>.TypeData: PTypeData;
begin
Result := TypInfo.GetTypeData(TypeInfo);
end;
class function TEnumeration<T>.IsEnumeration: Boolean;
begin
Result := TypeInfo.Kind=tkEnumeration;
end;
class function TEnumeration<T>.ToOrdinal(Enum: T): Integer;
begin
Assert(IsEnumeration);
Assert(SizeOf(Enum)<=SizeOf(Result));
Result := 0;
Move(Enum, Result, SizeOf(Enum));
Assert(InRange(Result));
end;
class function TEnumeration<T>.FromOrdinal(Value: Integer): T;
begin
Assert(IsEnumeration);
Assert(InRange(Value));
Assert(SizeOf(Result)<=SizeOf(Value));
Move(Value, Result, SizeOf(Result));
end;
class function TEnumeration<T>.MinValue: Integer;
begin
Assert(IsEnumeration);
Result := TypeData.MinValue;
end;
class function TEnumeration<T>.MaxValue: Integer;
begin
Assert(IsEnumeration);
Result := TypeData.MaxValue;
end;
class function TEnumeration<T>.InRange(Value: Integer): Boolean;
var
ptd: PTypeData;
begin
Assert(IsEnumeration);
ptd := TypeData;
Result := Math.InRange(Value, ptd.MinValue, ptd.MaxValue);
end;
class function TEnumeration<T>.EnsureRange(Value: Integer): Integer;
var
ptd: PTypeData;
begin
Assert(IsEnumeration);
ptd := TypeData;
Result := Math.EnsureRange(Value, ptd.MinValue, ptd.MaxValue);
end;
借助这些,您的代码将变为:
With that at hand your code becomes:
myTypeselection := TEnumeration<TMyType>.FromOrdinal(MyRadiogroup.ItemIndex);
这篇关于将整数值转换为枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!