2009-09-09 90 views
1

这是一个用于从DevExpress网格中的过滤器获取filtertype运算符的codesnippet: OperatorKindToStr用于从过滤器中提取operatorkind作为字符串并将其存储在xml文件中。 StrToOperatorKind用于将xml中的字符串转换为过滤器中的操作对象。将DevExpress TcxFilterOperatorKind转换为字符串还是从字符串转换?

const 
    CUSTFILTER_FILTERITEM  = 'FilterItem'; 

function OperatorKindToStr(const aOperatorKind: TcxFilterOperatorKind): string; 
begin 
    Result := 'foEqual'; 
    case aOperatorKind of 
    foEqual:  Result := 'foEqual'; 
    foNotEqual:  Result := 'foNotEqual'; 
    foLess:   Result := 'foLess'; 
    foLessEqual: Result := 'foLessEqual'; 

    // Plus a boring list of other constants 
end; 

function StrToOperatorKind(const aOpKindStr: string): TcxFilterOperatorKind; 
begin 
    Result := foEqual; 
    if aOpKindStr  = 'foNotEqual' then 
    Result := foNotEqual 
    else if aOpKindStr = 'foLess' then 
    Result := foLess 
    else if aOpKindStr = 'foLessEqual' then 
    Result := foLessEqual 
    else if aOpKindStr = 'foGreater' then 
    Result := foGreater 
    else if aOpKindStr = 'foGreaterEqual' then 
    Result := foGreaterEqual 

    // Plus a boring list of other if-else 
end; 

procedure UseStrToOperatorKind(const aFilterItem: IXmlDomElement); 
begin 
    if aFilterItem.nodeName = CUSTFILTER_FILTERITEM then 
    begin        // It is an FilterItem 
    vStr := VarToStr(aFilterItem.getAttribute(CUSTFILTER_COLPROP)); // Get the columnname 
    vOperatorKind := StrToOperatorKind(aFilterItem.getAttribute(CUSTFILTER_ITEMOPERATOR)); 
end; 

procedure UseOperatorKindToStr(const aFilterItem: TcxCustomFilterCriteriaItem); 
var 
    vStr: String; 
begin 
    if Supports(TcxFilterCriteriaItem(aFilterItem).ItemLink, TcxGridColumn, GridCol) then 
    vStr := OperatorKindToStr(TcxFilterCriteriaItem(aFilterItem).OperatorKind); 
end; 

显然我希望StrToOperatorKind和OperatorKindToStr有点聪明。 我已经尝试过VCL TypeInfo中的GetEnumProp方法,但它不起作用。 那么如何从aFilterItem变量中提取TcxFilterOperatorKind属性为字符串并返回到TcxFilterOperatorKind?

回答

1

使用GetEnumNameGetEnumValue作为梅森指出的二重奏。

而且你的功能应该成为更加简单:

function OperatorKindToStr(const aOperatorKind: TcxFilterOperatorKind): string; 
begin 
    Result := GetEnumName(TypeInfo(TcxFilterOperatorKind), Ord(aOperatorKind)); 
end; 

function StrToOperatorKind(const aOpKindStr: string): TcxFilterOperatorKind; 
begin 
    Result := TcxFilterOperatorKind(GetEnumValue(TypeInfo(TcxFilterOperatorKind), aOpKindStr)); 
end; 
+0

没错!这是正确的。感谢您的建议。 – 2009-09-10 19:49:32

1

GetEnumProp无法正常工作,因为它是您尝试执行的操作的错误功能。虽然你很接近。尝试GetEnumName和GetEnumValue,它们也在TypInfo单元中。