Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespaces in Delphi XE2

I've got two bpl packages: Core and Business, used by one application. Core.bpl contains unit User.pas with TUser class in it. TUser in Core.bpl has only two base fields: Login and Password. I want to expand TUser class in Business package with new field: UserName. And I want to name business unit as the base unit: User.pas.

So, I create a new unit User.pas in Business.bpl and place there TUser class that extends TUser from Core.bpl. Now I need to divide TUser from Core.bpl and TUser from Business.bpl. And I need to use "power of namespaces" here :)

I've read Embarcadero doc page. They say that one can set the default namespace for package with naming it, e.g. Base.Core. I named my packages as Base.Core.bpl and Extra.Business.bpl. And compiled files are named so. But all units in them are still named as they were before: User.pas -> User.dcu.

Now I've got two classes TUser in modules User.pas: one in package Base.Core.bpl, other in package Extra.Business.bpl. User.pas in Extra looks like

unit User;
interface
uses
  Base.User;
type
  TUser = class(Base.User.TUser)
  end;

But when I want to compile it, I've got a window: "Remove User. Unit(s) User were found in required package Base."

What have I do to inherit new TUser from Base.User.TUser like it can be in Java, for example?

P.S. Just in case, I use Delphi XE2 IDE.

like image 283
Mikhail Kopylov Avatar asked Dec 22 '12 19:12

Mikhail Kopylov


1 Answers

The default namespace appears to be Portal cake–it's a lie. The documentation you link to does not match the program.

I made this program:

MyCompany.Programs.MyProgram.dpr

program MyCompany.Programs.MyProgram;

uses
  MyUnit in 'MyUnit.pas';

begin
end.

MyUnit.pas

unit MyUnit;

interface

implementation

end.

And the resulting .dcu file is named MyUnit.dcu. According to the documentation that you linked to it should be named MyCompany.Programs.MyUnit.dcu.

I believe that you will have to specify the namespace explicitly in the unit name.

like image 61
David Heffernan Avatar answered Sep 17 '22 21:09

David Heffernan