Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid insert namespace in Delphi uses

Tags:

delphi

I manage a huge project in Delphi 2007. The target is to upgrade it to Delphi 10.1 Berlin this year. So in the meantime the source is compiled in both versions.

If there is a problem with the new Delphi we want the old version as backup.

My problem in unit dmActions.pas that is a unit inherited from TDataModule.

uses
  // VCL
  ActnList,
  ActnMan,
  Classes,
  Controls,
  Forms,
  Graphics,
  ImgList,
  Menus,
  SysUtils,
  XPStyleActnCtrls,
  Variants,
{$IFDEF BOLD_DELPHI16_OR_LATER}
  System.ImageList,
  System.Actions,
{$ENDIF}

  BusinessClasses;

Delphi IDE don't understand my IFDEF so it automatically insert missing units to this

uses
  // VCL
  ActnList,
  ActnMan,
  Classes,
  Controls,
  Forms,
  Graphics,
  ImgList,
  Menus,
  SysUtils,
  XPStyleActnCtrls,
  Variants,
{$IFDEF BOLD_DELPHI16_OR_LATER}
  System.ImageList,
  System.Actions,
{$ENDIF}

  BusinessClasses, System.ImageList, System.Actions;

But this don't compile in Berlin with this message

[dcc32 Error] dmActions.pas(36): E2004 Identifier redeclared: 'System.ImageList'
[dcc32 Error] dmActions.pas(36): E2004 Identifier redeclared: 'System.Actions'

And of course "System.ImageList, System.Actions" don't compile in D2007. So what is my best action to solve this ?

like image 730
Roland Bengtsson Avatar asked Oct 23 '18 06:10

Roland Bengtsson


Video Answer


2 Answers

You can make use of the Unit Aliases feature of Delphi here - at least as your Delphi 2007 supports dotted unit names in the first place. This allows to use the new unit names like System.SysUtils from Delphi 10.1 Berlin and still compile that project with Delphi 2007.

For this you have to add mappings to the Unit Aliases of the Delphi 2007 project like this:

System.SysUtils=SysUtils
System.Classes=Classes

For units that don't exist in Delphi 2007, like the ones you mention in your post, simply map to an existing unit:

System.Actions=ActnList
System.ImageList=ImgList

As a benefit you end up with uses clauses free of IFDEFs.

like image 180
Uwe Raabe Avatar answered Oct 17 '22 06:10

Uwe Raabe


As https://stackoverflow.com/users/2916756/nolaspeaker said it works by test compiler version directly. I used an inc-file and that don't work well in this case

But in my case I check Berlin so:

{$IFDEF VER310}
  System.ImageList,
  System.Actions,
{$ENDIF}
like image 2
Roland Bengtsson Avatar answered Oct 17 '22 06:10

Roland Bengtsson