views:

77

answers:

1

This is a spin off from my last question How does delphi convert ModalResult properties?

Since Delphi doesn't convert ModalResult properties, what's the best way for me to convert ModalResult properties to integers?

I don't really want:

If SpecialCase then
else if AnotherSpecialCase then
else BehaveNormally

So how do I convert values such as 'mrOk' into 1?

Note: I'm using

PropInfo := GetPropInfo(Instance, PropertyName);
SetPropValue(Instance, PropInfo, PropertyValue);

to set the property values.

Delphi 2007

+3  A: 

There is no converter for ModalResults, Delphi stores the Integer representation in the DFM. As a solution I've registered a new converter

const
  ModalResults: array[0..10] of TIdentMapEntry = (
    (Value: mrNone; Name: 'mrNone'),           
    (Value: mrOk; Name: 'mrOk'),               
    (Value: mrCancel; Name: 'mrCancel'),       
    (Value: mrAbort; Name: 'mrAbort'),         
    (Value: mrRetry; Name: 'mrRetry'),         
    (Value: mrIgnore; Name: 'mrIgnore'),       
    (Value: mrYes; Name: 'mrYes'),             
    (Value: mrNo; Name: 'mrNo'),               
    (Value: mrAll; Name: 'mrAll'),             
    (Value: mrNoToAll; Name: 'mrNoToAll'),     
    (Value: mrYesToAll; Name: 'mrYesToAll'));



function ModalResultToIdent(ModalResult: Longint; var Ident: string): Boolean;
begin
    Result := IntToIdent(ModalResult, Ident, ModalResults);
end;

function IdentToModalResult(const Ident: string; var ModalResult: Longint): Boolean;
begin
    Result := IdentToInt(Ident, ModalResult, ModalResults);
end;
initialization
    RegisterIntegerConsts(TypeInfo(TModalResult), IdentToModalResult, ModalResultToIdent);
JamesB