6
From the 2009 version of Delphi (if I’m not mistaken) came the namespaces.
I would like to know, in a simple example if possible that illustrates a case of advantage, what would be the advantages of namespaces in Delphi?
Explaining a little about namespaces, on C# we could have three files containing the same namespace
:
Filing cabinet Cs accounts.:
namespace Projeto.Forms {
public class ContasForm : Form {
...
}
}
Filing cabinet Compras.Cs:
namespace Projeto.Forms {
public class ComprasForm : Form {
...
}
}
Filing cabinet Customer.Cs:
namespace Projeto.Forms {
public class ClienteForm : Form {
...
}
}
In a fourth file, Mainform.Cs, I would have accessed the three classes only by references Projeto.Forms
:
using Projeto.Forms;
namespace Projeto {
public class ContasForm : Form {
private ContasForm contasForm;
private ComprasForm comprasForm;
private ClienteForm clienteForm;
}
}
Already in Delphi this does not occur:
Filing cabinet Projeto.Forms.Contas.pas:
unit Projeto.Forms.Contas;
interface
uses ...
type
TContasForm = class(Form)
...
Filing cabinet Projeto.Forms.Compras.pas:
unit Projeto.Forms.Compras;
interface
uses ...
type
TComprasForm = class(Form)
...
Filing cabinet Project.Forms.Client.pas:
unit Projeto.Forms.Cliente;
interface
uses ...
type
TClienteForm = class(Form)
...
Anyway, when wanting to refer them to Project.Forms.Mainform I need to do as follows:
unit Projeto.Forms.MainForm;
interface
uses Projeto.Forms.Contas, Projeto.Forms.Compras, Projeto.Forms.Cliente;
type
TMainForm = class(Form)
...
Not it is possible to do something like uses Projeto.Forms;
and from there have access to the three classes. Much less is it allowed in Delphi to have more than one file with the same name.
The only way I’ve seen of having such a benefit would be to manipulate. Example:
Create another Unit with the name Projeto.Forms.pas:
unit Projeto.Forms;
interface
type
TContasForm = Projeto.Forms.Contas.TContasForm;
TComprasForm = Projeto.Forms.Compras.TComprasForm;
TClienteForm = Projeto.Forms.Cliente.TClienteForm;
...
And then make the reference as I had quoted, uses Projeto.Forms;
and then have access to the three classes.
Well, I don’t know if that would have consequences and, of course, it was just to illustrate.
In light of this, I ask: What are the advantages of using namespaces in Delphi?
I ask for a small example just to illustrate.
Grateful!
This question is specific to C# but the concept is the same or very similar: http://answall.com/questions/11199/comorfunction-namespaces-no-c
– Caffé
@Caffe, in the Delphi I guess you can’t say that namespaces directly bring the same utility/functionality.
– JamesTK
What would be the difference between Delphi namespaces and C namespaces#?
– Caffé
@Caffe, boy, worse than the only thing I can think of right now is that I can’t have the same namespace in more than one file. Unless I manipulate, for example: in
Modelos.Usuario
me dotype TTipoAcesso = Modelos.Usuario.TipoAcesso.TTipoAcesso;
. So, in Java and C#, for example, in more than one file you can have namespaces and thereby create a "bundle" of classes/types.– JamesTK
By file you refer Packages or Units? Having the same namespace for different Units is just the function of namespace, or it would not be a namespace but only a Unit with a big name. As for declaring the same namespace in different Packages (different BPL files), I haven’t used Delphi for a few years and I don’t have any to check, but I don’t see any reason why I can’t. You tried to?
– Caffé
@Caffé, by my attempts, can only have one namespace for Unit, since the file has to have the same name as the Unit. So yes, it’s just a file with a big name. Example:
Projeto.Forms.MainForm.pas
. I didn’t mean anything about Packages yet.– JamesTK
No, it’s not just a file with a big name. It’s a namespace in fact, with all or many of the benefits of those C# responses that I inked in my first comment. See, in Project.Forms.Mainform.pas, the namespace is Projeto.Forms, and in it I can have as many Units as I want: Projeto.Forms.Contas.pas, Projeto.Forms.Compras.pas, Project.Forms.Client.pas...
– Caffé
@Caffé, if I were to make use of them in another Unit, example in
Projeto.Forms.MainForm.pas
I would have to add:uses Projeto.Forms.Compras, Projeto.Forms.Cliente, Projeto.Forms.Contas;
. I mean, file by file the same way I would:uses UntContasForm, UntComprasForm, UntClienteForm;
. You can’t just adduses Projeto.Forms;
and then have access to all classes of the three files (TClienteForm, TComprasForm, TContasForm
). Clarifying that I’m talking about three files and each containing a class, to make sense.– JamesTK
With this last comment and the improvement of the question I understood your point. While in C# types are logically grouped in a namespace, in Delphi types are grouped in a Unit, and namespaces are not groupings of types but groupings of Units. Later I post an answer.
– Caffé