2009-09-09 91 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單元中。