1. $ ifdef is equivalent to $ if defined (...):
They end with: $ endif and $ ifend. In this example, ver200 is the ID of Delphi 2009.
Begin {$ ifdef ver200} showmessage ('this is Delphi 2009 '); {$ endif} {$ if defined (ver200)} showmessage ('this is Delphi 2009 '); {$ ifend} end;
2. $ ifndef is equivalent to $ if not defined (...):
They end with: $ endif and $ ifend. In this example, ver150 is the ID of Delphi 7.
Begin {$ ifndef ver150} showmessage ('this is not Delphi 7'); {$ endif} {$ if not defined (ver150)} showmessage ('this is not Delphi 7 '); {$ ifend} end;
3. You can use or and:
Begin {$ define AAA} {$ define BBB} {$ if defined (AAA) or defined (BBB)} showmessage ('condition identifier AAA and one of BBB defines '); {$ ifend} {$ if defined (AAA) and defined (BBB)} showmessage ('condition identifiers AAA and BBB both define '); {$ ifend} end;
4. You can use constants in the system unit:
I tested many CONSTANTS IN THE SYSTEM unit.
Begin showmessage (floattostr (compilerversion); {in Delphi 2009, compilerversion = 20.0 }{$ if compilerversion >=17.0} showmessage ('this is a version of Delphi 2005 or later '); {$ ifend} end;
5. Use $ ifopt to determine the compilation switch:
Delphi is quite fun. 26 letters are arranged into different switch commands (use Ctrl + O to view them, of course, more than these switch commands );
$ Ifopt can determine whether these commands are enabled.
This command is not very commonly used. I checked the VCL source code of 2009 and used it six times in total.
Begin {$ ifopt B +} showmessage ('COMMAND B has opened '); {$ else} showmessage ('COMMAND B closed '); {$ endif} {$ B +} {$ ifopt B +} showmessage ('OK! '); {$ Endif} end;