Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help w/ Namespaces/Classes

Status
Not open for further replies.

stillinlife101

Programmer
Feb 15, 2005
29
US
I want to write a series of namespaces and classes that look something like this:

namespace a
{
class a
{
public int DoSomething()
{
}
}
namespace b
{
class b:a
{
public int DoSomethingAlso()
{
}
}
}
}

and then be able to declare something as such:

a.b NewVar = new a.b();
NewVar.DoSomething();
NewVar.DoSomethingAlso();

Is something like that possible, and if so, could somebody give me a template? Thanks,

Dan
 
You generally don't want to name the class the same as your namespace, because then you get fully qualified names like a.a and b.b, which is confusing.

There's two ways to go -- you can fully-qualify the name of a inside the b namespace:
Code:
using System;

namespace b
{
  public class b : a.a
  {
    public b()
    {
    }
  }
}
or use an using statement, which allows you to use shortcut names:
Code:
using System;
using a;

namespace b
{
  public class b : a
  {
    public b()
    {
    }
  }
}
Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
How do you nest namespaces? I'm working on something that will require a hierarchy of namespaces, sort of like the System namespace in .Net. System.H1.H2.H3... etc How do I create those nested namespaces so that I can

1) use the hierarchy to find my class and
2) inherit from higher level classes? (all namespaces should inherit from the parent namespace, i.e. H2 inherits from H1, etc)

Dan
 
You would create nested directories and/or projects under your solution. By setting the default namespace at the project level (project properties), VS.NET will automatically create the correct namespace whenever you add a new class.

If you have multiple namespaces within a project, you can create folders in your VS.NET project (right-click, add new folder). You'll get the same nested namespace, and nested directory structure, but it'll all be in one assembly when it gets built.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top