TTransactionType = (ttNone, ttCash, ttCheck, ttDebit);
TTransactionTypeHelper = record helper for TTransactionType
public
class function ToTransactionType(TranTypeDescription : string) : TTransactionType;
function ToString(): string;
end;
function TTransactionTypeHelper.ToString: string;
begin
case Self of
ttCash:
Result := 'Cash';
ttCheck:
Result := 'Check';
ttDebit:
Result := 'Debit'
else
Result := '';
end;
end;
class function TTransactionTypeHelper.ToTransactionType(
TranTypeDescription: string): TTransactionType;
begin
if (TranTypeDescription = 'Cash') then
Result := ttCash
else if (TranTypeDescription = 'Check') then
Result := ttCheck
else if (TranTypeDescription = 'Debit') then
Result := ttDebit
else
Result := ttNone;
end;
可以通过TTransactionTypeHelper(预期)访问类方法ToTransactionType。
有没有办法直接通过枚举使方法ToTransactionType可访问?如,
TTransactionType.ToTransactionType('Cash');
答案 0 :(得分:2)
正如@Victoria在评论中提到的那样,向ToTransactionType
方法添加静态,会使调用TTransactionType.ToTransactionType('Cash')
正常工作。
如果要在不编写帮助程序的情况下扩展枚举类型,则无法实现。但还有另一种方式:
使用RTTI和单位TypInfo.Pas
,您可以拨打GetEnumValue():
var
i : Integer;
myTransactionValue : TTransactionType;
begin
i := GetEnumValue(TypeInfo(TTransactionType),'ttCheck');
if (i <> -1) then myTransactionValue := TTransactionType(i);
end;
s := GetEnumName(TypeInfo(TTransactionType),Ord(TTransactionType.ttCheck)); // s = 'ttCheck'