Newbie: namespace query

  • Thread starter Thread starter MM
  • Start date Start date
M

MM

Hi,

Okay, I think I understand nested namespaces in the same c# file. But
how do you 'nest' across multiple source files?

eg:
//all in one file

namespace toplevel
{
namespace stuff1
{
classes and stuff
}

namespace stuff2
{
classes and stuff
}
}

but how do you spread out the same hierarchy across multiple source files?
//file1
namespace toplevel
{
classes and stuff
}

//file2
namespace toplevel.stuff1
{
classes and stuff

}

//file3
namespace toplevel.stuff2
{
classes and stuff
}

//mainapp file
//so that you can write

using toplevel;
etc
static void Main()
{
stuff1.method
stuff2.method
etc...

Thanks for your help. matthew.
 
Hi MM,
if you split across multiple files like so:

//file1
namespace toplevel
{
class clsTopLevel
{
}
}

//file2
namespace toplevel.stuff1
{
class clsStuffOne
{
}
}

//file3
namespace toplevel.stuff2
{
class clsStuffTwo
{
}
}


The in your main app you need to include all of your namespaces like:

using toplevel;
using topLevel.stuff1;
using topLevel.stuff2;

static void Main()
{
clsTopLevel a = new clsTopLevel();
clsStuffOne b = new clsStuffOne();
clsStuffTwo c = new clsStuffTwo();
}


Infact even if you put multiple nested namespaces in one file like you had
in your first example you would have to include the nested namespaces as well
into your project i.e.:

namespace toplevel
{
class clsTopLevel
{
}


namespace stuff1
{
class clsStuffOne
{
}
}


namespace stuff2
{
class clsStuffTwo
{
}
}
}

Your main program would still need to import:

using toplevel;
using topLevel.stuff1;
using topLevel.stuff2;



Hope that helps
Mark.
 
Hi Mark, Brilliant. Many thanks for the reply. I've changed my files to
reflect this structure.

One last question though- when you have "using System;" you can access
all the underlying namespaces but if I try "using toplevel;" no
namespaces defined 'lower' are visible. Fully qualified like
"toplevel.stuff1.someclassmethod" and also what you've typed works fine.
What am I missing? Thanks Matthew.
 
Hi MM,
when you import System you will see all of the items that live inside the
System namespace but you won't see other namespaces automatically that are
lower down unless you include them or use the fully qualified name.

For example you have System and System.Threading if I import System and try
to say the following in my code:

Threading.Thread

then I get a compile error, I have to say:

System.Threading.Thread

or include System.Threading at the top of the file.

Hope that helps
Mark.
 
Mark said:
Hi MM,
when you import System you will see all of the items that live inside the
System namespace but you won't see other namespaces automatically that are
lower down unless you include them or use the fully qualified name.

For example you have System and System.Threading if I import System and try
to say the following in my code:

Threading.Thread

then I get a compile error, I have to say:

System.Threading.Thread

or include System.Threading at the top of the file.

Hope that helps
Mark.
Hi Mark,

Okay - I've got it. Thanks very much for your help. matthew.
 
Back
Top