Domanda

Ecco un frammento di codice che utilizzo per ottenere l'operatore filtertype da un filtro in una griglia DevExpress: OperatorKindToStr viene utilizzato per estrarre operatorkind da un filtro come stringa e memorizzarlo in un file xml. StrToOperatorKind viene utilizzato per riconvertire una stringa da xml per impostare un operatorkind in un filtro.

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;

Apparentemente voglio che StrToOperatorKind e OperatorKindToStr siano un po 'più intelligenti. Ho provato il metodo GetEnumProp in VCL TypeInfo ma non funzionerà. Quindi, come posso estrarre la proprietà TcxFilterOperatorKind da una variabile aFilterItem in una stringa e tornare a un TcxFilterOperatorKind?

È stato utile?

Soluzione

Usa il duetto GetEnumName e GetEnumValue come ha sottolineato Mason.

E le tue funzioni dovrebbero diventare molto più semplici:

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;

Altri suggerimenti

GetEnumProp non ha funzionato perché è la funzione sbagliata per quello che stai cercando di fare. Sei vicino, però. Prova GetEnumName e GetEnumValue, che si trovano anche nell'unità TypInfo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top