类似 char xxx[] = {"zero", "one", "two", "three",};
如果不是写一个函数类似
char f(int x)
{
switch (x)
{
case 1: return "one";
case 5: return "five";
case 10: return "ten";
}
return "unknown";
}
class Program
{
static void Main(string[] args)
{
Color[] colors = EnumGetValues(typeof(Color)) as Color[];
Random random = new Random();
Color color = colors[randomNext(0, colorsLength)];
}
}
internal enum Color
{
White,
Black,
Red,
Green,
Pink
}
单元:TypInfo
函数:function GetEnumName(TypeInfo: PTypeInfo; Value:Integer):string;
说明:
Returns the name of a Delphi enumerated type constant given its value
Use GetEnumName to convert a Delphi enumerated value into the symbolic name that represents it in code
TypeInfo is the type information record that describes the enumerated type You can obtain the type information for a type by calling the TypeInfo function in Delphi or the __delphirtti operator in C++
Value is the enumerated value, represented as an integer
GetEnumName returns the string that represents the specified value
示例:
列举枚举类型DBTUpdateAction的第一个枚举项的名称。
var
Info: PTypeInfo;
name: string;
begin
Info := SystemTypeInfo(DBTUpdateAction);
name := TypInfoGetEnumName(Info, Ord(0));
end;
以上就是关于如何通过枚举类型来获得数值相对应的字符串全部的内容,包括:如何通过枚举类型来获得数值相对应的字符串、在C#中定义一个枚举类型,怎么在这个枚举中随机获取一个元素,然后在剩下的元素中继续随机获取元素啊、delphi里的枚举类型中获取元素的name的函数如: Name:=GetEnumName(TypeInfo(TEnums),Ord(Enum));等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)