1

i have understood that a namespace is a container of units; but i haven't understood well as work with it. I try to explain better, about what i want tell. For example i have four units:

1) Animals               // es: class TAnimals
2) Animals.Dog           // es: class TDog
3) Animals.Cat           // es: class TCat
4) Animals.Cat.Female    // es: class TFemale

Of course, this four units are in four different file. If i make new unit and need use all it, i write:

uses 
  Animals, Animals.Dog, Animals.Cat, Animals.Cat.Female

Now, until are very few problem not there is, but when are very much? So i wanted to know as i can manage better, making one only unit: Animal (namespace root) that contain all other, having so something as:

uses
  Animals;

And to have access from Animal to all other classes defined in all other namespace of second,third etc level, for example:

program Project1;
uses 
  Animals;
var 
  x: Animals;
begin
  x := TAnimals.Cat.Female.Create;
  try
    ....
  finally
    x.Free;
  end;
end.

Searching in internet, maybe i have found something using interface, but i have impression that not correct solution becouse in my opinion is much complicated for something that with delphi xe2 is native. Much probably i mistake, or it do refer to some older version of delphi; sincerly i don't know. But i wanted know as i do to do it, of course if possibile. Thanks again very much.

razlebe
  • 7,134
  • 6
  • 42
  • 57
Marcello Impastato
  • 2,263
  • 5
  • 30
  • 52
  • I wouldn't like to have that as it means that every unit found in the search path that starts with "Animals." would be included silently even when it is never used. No, I definitely don't like that! – Uwe Raabe Sep 15 '11 at 20:31

1 Answers1

2

Basically XE2 just renames RTL units to names with a dot in it, and to try also to load units with a dotted prefix specified on the command line.

The ability to have units with a dot in it existed already in Delphi for a long time.

Forget whatever you know about namespaces, Delphi XE2 is not like that. Moving a unit in the namespace hierachy means renaming it (changing the prefix), contrary to e.g. Java where the container/file/class has a certain name, and only its position signals the place in the namespace hierarchy.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
  • 1
    The last sentence is wrong, as in addition the Java class file (sitting in the proper directory) also has to specify its package name in the first code line (package com.example; ... public class Bzz ...) - so if you have the file, you also know in which directory it has to be. – mjn Sep 16 '11 at 07:01
  • Default namespaces (called Unit space names in XE2) can also be specified in the project options dialogs - not only on the command line – mjn Sep 16 '11 at 07:11
  • Well, that is the same thing essentially. Point is that it is not within source (IOW within unit/module scope, like e.g. some of Pascal successors do, but also Java with import x.y.*) – Marco van de Voort Sep 16 '11 at 09:48