简体   繁体   中英

How can I mark a property as deprecated in delphi?

I want to mark a property in Delphi as deprecated for removal later. According to the Delphi documentation deprecated may be appended to any declaration, but it's not working for properties. Is there a way to do this?

This is what I tried:

property SomeProp: string
  read   FSomeProp
  write  SetSomeProp; deprecated 'Use SomeOtherProp instead';

No, this is not possible. According to the documentation ,

The 'hint' directives platform , deprecated , and library may be appended to any declaration. These directives will produce warnings at compile time. Hint directives can be applied to type declarations, variable declarations, class, interface, and structure declarations, field declarations within classes or records, procedure, function, and method declarations, and unit declarations.

You can't do that, however You could write a deprecated code in the property setter/getter!

so in your case you should'v create a SetSomeProp setter like this:

Type
    TYourClass = class
    private
      procedure DummyDepricated; deprecated 'Use SomeOtherProp instead';
      procedure SetSomeProp(const AValue: string); 
    published
      property SomeProp: string read   FSomeProp write  SetSomeProp; 

    implementation

    procedure TYourClass.SetSomeProp(const AValue: string); 
    begin
      DummyDepricated;
      //the old setter code here
    end;

    procedure TYourClass.DummyDepricated;
    begin
      //this is dummy
    end;

If your property were read-only, then you could use a simple workaround.

The code would go from this:

property SomeProp: string read FSomeProp; deprecated 'Use SomeOtherProp instead';

to this:

function SomeProp: string; deprecated 'Use SomeOtherProp instead';

If it were me, i would go the route of:

function SomeProp: string; deprecated 'Use SomeOtherProp instead';
procedure SetSomeProp(Value: string); deprecated 'Call SomeOtherProp := Value instead';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM