Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing specific class type reference in Delphi attribute-constructor
    text
    copied!<p>As Delphi cannot handle attributes on enum-values, I tried another approach. TMyEnum is my enum. Class TMyEnumLabelProvider provides the labels I need. I link the enum-type with an attribute to its labelprovider. This doesn't compile however. I cannot define the provider fully before referencing its classtype since it uses TMyEnum in one of its methods. </p> <pre><code>TEnumLabelProviderCallback = procedure(Context: TObject; Index: integer; const Name: string) of object; TEnumLabelProvider = class abstract public procedure Iterate(Context: TObject; Callback: TEnumLabelProviderCallback); virtual; abstract; end; TEnumLabelProviderClass = class of TEnumLabelProvider; TEnumLabelProviderAttribute = class(TCustomAttribute) private FProviderClass: TEnumLabelProviderClass; public constructor Create(ProviderClass: TEnumLabelProviderClass); property ProviderClass: TEnumLabelProviderClass read FProviderClass; end; TMyEnumLabelProvider = class; {$SCOPEDENUMS ON} [TEnumLabelProvider(TMyEnumLabelProvider)] TMyEnum = (MyEnum0, MyEnum1, MyEnum2); {$SCOPEDENUMS OFF} // This is where the compilation fails..... TMyEnumLabelProvider = class(TEnumLabelProvider) public class function GetLabel(MyEnum: TMyEnum): string; procedure Iterate(Context: TObject; Callback: TEnumLabelProviderCallback); override; end; </code></pre> <p>Any ideas on how to do this properly. For now I workaround by declaring GetLabel(MyEnum: integer), but obviously I prefer being type strict.</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload