473,320 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Namespaces being stupid

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!
Nov 15 '05 #1
6 1333
"Justin" <jh***@stanford.edu> wrote in message
news:67**************************@posting.google.c om...
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
--
http://www.csharp-station.com
Nov 15 '05 #2
"Justin" <jh***@stanford.edu> wrote in message
news:67**************************@posting.google.c om...
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
--
http://www.csharp-station.com
Nov 15 '05 #3
jh***@stanford.edu (Justin) wrote in
news:67**************************@posting.google.c om:
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/de...l=/library/en-
us/csref/html/vclrfNamespaces.asp

or

http://tinyurl.com/ptsq
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #4
jh***@stanford.edu (Justin) wrote in
news:67**************************@posting.google.c om:
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/de...l=/library/en-
us/csref/html/vclrfNamespaces.asp

or

http://tinyurl.com/ptsq
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #5
On Sun, 5 Oct 2003 18:59:37 -0600, "Joe Mayo"
<jm***@ddiieessppaammeerrssddiiee.com> wrote:

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.

Nov 15 '05 #6
"Jekke, Just Jekke" <je***@REMOVEALLinsidejoke.tvCAPS> wrote in message
news:6s********************************@4ax.com...
On Sun, 5 Oct 2003 18:59:37 -0600, "Joe Mayo"
<jm***@ddiieessppaammeerrssddiiee.com> wrote:

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.


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
--
http://www.csharp-station.com
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
24
by: Marcin Vorbrodt | last post by:
Here is an example of my code: //Header file #include <vector> using std::vector; namespace Revelation { // class definitions, etc... // class members are of type std::vector }
4
by: Sean Quinn | last post by:
Recently I've come across an oddity in .NET, going back to a Managed C++ implementation for GUIs since I never really learned how to code in MFC. Any way, I attempted to put some standard code I...
0
by: Justin | last post by:
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...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
7
by: Paul | last post by:
Hello, I'm coming from a PHP background and am working on a large-scale VB.NET project. One of the cornerstones of this project is a reusable form class, to standardize our web forms. My goal...
2
by: Lore Leunoeg | last post by:
Hello I'm using c# XmlDocument class to add new XHTML-Nodes to my website. Unfortunately XmlDocument always adds an unwanted empty namespace attribute xmlns="" to every new Element. These empty...
7
by: Peter Kirk | last post by:
Hi, there is a logging service called "log4net". It uses namespaces such as "log4net" and "log4net.Appender". If I am writing my own software, would it be considered bad style to use...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.