Email Aaron Blake now at webmaster@aaronblake.co.uk
or contact me

Duplicate Namespace Problem

Today I cam across an annoying Namespace issue, which I spent ages trying to solve.

If you have a namespace which has the same name as another namespace but at a different level, .net won’t know which namespace to use, and will through an error.

This is also true for classes as well.

Example:

namespace Zone.Section
{
public class ShowZone{}
}


using Zone.Section

namespace Website.Zone
{

public class Page
{
void Load()
{
Zone.Section.ShowZone();
}

}

}

Now the example above will not compile, because .net can only see the Zone for Website.Zone, and not Zone.Section.

This problem usually occurs if you haven’t organised your namespaces properly, or is you have inherited code form various sources.

The solution

To get this code to work, you need to use the global prefix, and define the “using” (or Imports in vb.net) within the namespace.

For example:

using Zone.Section

namespace Website.Zone
{
using Zone = global::Zone;

public class Page
{
void Load()
{
Zone.Section.ShowZone();
}

}

}

OR

namespace Website.Zone
{

public class Page
{
void Load()
{
global::Zone.Section.ShowZone();
}

}

}

Post a Comment

You must be logged in to post a comment.

Email me through this magic form.