Compiling for different Delphi versions

I have a unit that I use in a Delphi XE project as well as a Delphi 11 (Alexandria) project.

One of the procedures is a handler for reading data for TIdUDPServer and the definition is different for XE vs 11.The only difference is the const keyword in the later version. I got around that by doing this:

{$IF CompilerVersion > 22} // Later than Delphi XE
    procedure UDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle);
    procedure UDPRead(AThread: TIdUDPListenerThread; AData: TIdBytes; ABinding: TIdSocketHandle);

Is there a better way?

Not really, I guess you could do it inline

{$IF CompilerVersion > 22}
{$LEGACYIFEND ON} //to stop the compiler warning about ifend

procedure UDPRead(AThread: TIdUDPListenerThread; {$IFDEF XE2UP} const {$IFEND} AData: TIdBytes; ABinding: TIdSocketHandle);

Ah, of course! I hadn’t thought about defining a label further up and using it inline.

I had to change the {$IFEND} to {$ENDIF} for the inline code to get it to compile though.

Thanks for that. Something useful learned for the day. :slight_smile:

If you use any 3rd party libraries, they probably have ready made defines for this already. Eg, for NexusDB you’ll find a file with such defines.