uses TypInfo;
type
TCommType = (cEm, cDm, cMd);
TCommTypeCon = class
public
class function CommToStr(nComm: TCommType): string;
class function StrToComm(const nStrComm: string): TCommType;
end;
implementation
class function TCommTypeCon.CommToStr(nComm: TCommType): string;
begin
Result := GetEnumName(TypeInfo(TCommType), Ord(nComm));
end;
class function TCommTypeCon.StrToComm(const nStrComm: string): TCommType;
begin
Result := TCommType(GetEnumValue(TypeInfo(TCommType), nStrComm));
end;
````
集合和字符串互转
集合:
type
TSocketState = (ssDisconnecting, ssDisconnected, ssConnected, ssConnecting, ssListening, ssAccepting);
const
TSocketStateCaption: array[TSocketState] of String = ('正在断开', '已经断开', '已经连接', '正在连接', '正在侦听', '正在接入');
type
TCommandType = (ctEmptyCommand, ctAdd, ctModify);
TCommandTypeConvert = class
public
class function CommandToString(ACommand: TCommandType): string;
class function StringToCommand(const AStrCommand: string): TCommandType;
end;
implementation
class function TCommandTypeConvert.CommandToString
(ACommand: TCommandType): string;
begin
Result := GetEnumName(TypeInfo(TCommandType), Ord(ACommand));
end;
class function TCommandTypeConvert.StringToCommand(const AStrCommand: string)
: TCommandType;
begin
Result := TCommandType(GetEnumValue(TypeInfo(TCommandType), AStrCommand));
end;
View Code
调用方法:
str:=GetEnumName(TypeInfo(TSocketState),Ord(lvClient.SocketState));
转载于:https://www.cnblogs.com/stroll/p/7988434.html
````
在 delphi 里,提供了一个 TypeInfo 单元,用于运行期获取变量、结构的信息数据。
试编写代码示例如下:
集合定义:
type
TWeekDays = (星期一,星期二,星期三,星期四,星期五,星期六,星期日);
TDays = set of TWeekDays;
集合转换成字符串:
procedure TForm1.Button1Click(Sender: TObject);
var
ti: PTypeInfo;
td: PTypeData;
i: Integer;
begin
ti := TypeInfo(TWeekDays);
td := GetTypeData(ti);
for i := td^.MinValue to td^.MaxValue do
Memo1.Lines.Add(GetEnumName(ti, i));
end;
https://zhidao.baidu.com/question/138947005851490125.html
````
var
sd: set of 1..9;
t: set of 1..60;
begin
sd := []; // sd = []
sd := [1,2]; // sd = [1,2]
sd := sd + [15]; // sd = [1,2,15] 按照定义,sd应该为1到9的值,
// 15应该不能加进来,但实际能加进去,使用 Include(sd, 15)也是一样
sd := sd + [16]; // sd = [1,2,15] 发现大于16的数字无法添加进集合内
sd := sd + [25]; // sd = [1,2,15]
sd := [];
showmessage(inttostr(sizeof(sd))); // 输出 2
showmessage(inttostr(sizeof(t))); // 输出 8
end;
showmessage(inttostr(PWord(@sd)^))
PWord(@sd)^ := 106;
https://www.cnblogs.com/tangqs/p/3264821.html
````
https://www.cnblogs.com/del/archive/2007/11/30/978683.html
https://www.cnblogs.com/del/archive/2007/11/30/978673.html
https://www.cnblogs.com/del/archive/2007/11/30/978672.html