Namespaces being stupid

J

Justin

I've made some controls that have namespaces and I have trouble with
the "using namespace" line, it doesn't do anything. Here's what's
going on:

1). I have a class like this:

namespace N1.N2
{
class A{};
}

2). I reference the project with N1.N2 from another project's file
where I want to use my class I have:

using N1;
using N1.N2;

namespace N3
{
// Inside the code somewhere ...
N2.A test = new N2.A(); // This line does not work
N1.N2.A test = new N1.N2.A(); // This DOES work

Why can't I make use of the "using namespace" line to shorten my code?
I don't want to have to write out all the N1.N2.xxxx stuff, it's
annoying and makes my code look messy. There are not naming conflicts
with other namespaces.

Help please!

Thanks!
 
J

Joe Mayo

Justin said:
I've made some controls that have namespaces and I have trouble with
the "using namespace" line, it doesn't do anything. Here's what's
going on:

1). I have a class like this:

namespace N1.N2
{
class A{};
}

2). I reference the project with N1.N2 from another project's file
where I want to use my class I have:

using N1;
using N1.N2;

namespace N3
{
// Inside the code somewhere ...
N2.A test = new N2.A(); // This line does not work
N1.N2.A test = new N1.N2.A(); // This DOES work

Why can't I make use of the "using namespace" line to shorten my code?
I don't want to have to write out all the N1.N2.xxxx stuff, it's
annoying and makes my code look messy. There are not naming conflicts
with other namespaces.

Hi Justin,

A using statement only allows you to reference types, not nested namespaces.
The dot operator in the N1.N2 namespace declaration makes N2 a nested
namespace. If you want a shortcut, you could try an alias:

using aliasN2 = N1.N2;

aliasN2.A test = new aliasN2.A();

Joe
 
C

Chris R. Timmons

(e-mail address removed) (Justin) wrote in
I've made some controls that have namespaces and I have trouble
with the "using namespace" line, it doesn't do anything. Here's
what's going on:

1). I have a class like this:

namespace N1.N2
{
class A{};
}

2). I reference the project with N1.N2 from another project's
file where I want to use my class I have:

using N1;
using N1.N2;

namespace N3
{
// Inside the code somewhere ...
N2.A test = new N2.A(); // This line does not work
N1.N2.A test = new N1.N2.A(); // This DOES work

Why can't I make use of the "using namespace" line to shorten my
code?
I don't want to have to write out all the N1.N2.xxxx stuff,
it's
annoying and makes my code look messy. There are not naming
conflicts with other namespaces.

Justin,

I don't think you need the "using N1" statement. All you should need
is:

using N1.N2;

namespace N3
{
... inside a class somewhere ...
A test = new A();
}

"using" directives in C# are very simple when compared to languages
like Java and Python. There are no wildcards and the namespace name
has no dependency upon the folder the .cs file is stored in. If
you're familiar with Delphi, C# "using" directives are very similar
to Delphi's "uses" clause.

The "dot" (.) in an individual namespace name has no special
significance, other than to separate words in the namespace name.

See the docs for more info and examples:

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/csref/html/vclrfNamespaces.asp

or

http://tinyurl.com/ptsq


Hope this helps.

Chris.
 
J

Jekke, Just Jekke

A using statement only allows you to reference types, not nested namespaces.
The dot operator in the N1.N2 namespace declaration makes N2 a nested
namespace. If you want a shortcut, you could try an alias:

using aliasN2 = N1.N2;

aliasN2.A test = new aliasN2.A();

Actually, his problem is that the dot in .net namespaces is not really
a nesting operator. It is not safe to assume that the N1.N2 namespace
is inside of the N1 namespace.
 
J

Joe Mayo

Jekke said:
Actually, his problem is that the dot in .net namespaces is not really
a nesting operator. It is not safe to assume that the N1.N2 namespace
is inside of the N1 namespace.

That is not what I said. N2 is nested in N1. Looking at the original
poster's code:

namespace N1.N2
{
class A{};
}

is semantically equivalent to:

namespace N1
{
namespace N2
{
class A{};
}
}

according to the C# Specification:

http://www.jaggersoft.com/csharp_standard/16.2.htm

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top