namespaces colliding

A

Anders Eriksson

I need to use two namespaces that unfortunately has the same sub-namespace.

using System.Windows.Forms
using laserengineLib;

the laserengineLib contains a System namespace.

Now when I try to use a MessageBox.Show(msg); I get this error:

Error 1 The name 'MessageBox' does not exist in the current context


I then try with
System.Windows.Form.MessageBox.Show(msg);

and I get this error:

Error 2 'laserengineLib.System' does not contain a definition for 'Windows'

How can I specify that I want to use the standard System.Windows and not
the one in laserengineLib?

// Anders
 
K

kndg

The code you posted should work either way. And in fact, when I attempt
to recreate the problem, I find that it works just as I expect. So
whatever the problem is, either you did not post anything like the
actual code you're trying to compile, or the problem is in code you
didn't post.

You should post a concise-but-complete code example that reliably
demonstrates the problem.

Now, all that said, having a namespace "System" within another namespace
is a terrible design. I hope that you are not the person who made the
decision to use that name, but if you are, you definitely should use a
different name. In spite of the apparent hierarchical nature of
namespaces, because of the way namespaces can be imported, you should
try to avoid duplicate namespace names even if in different hierarchies,
and this is especially true for namespace names that are practically
guaranteed to be in scope, such as "System".

There are ways to deal with conflicts, such as fully qualifying type
names to resolve the ambiguity, but it's always better if your code can
avoid needing those work-arounds in the first place.

I think his code would be similar to this,

namespace laserengineLib
{
class System
{
}

public class MyClass
{
public static void Main(string[] args)
{
System.Windows.Forms.MessageBox.Show("Test");
}
}
}

He can always workaround this problem by adding global,

public static void Main(string[] args)
{
global::System.Windows.Forms.MessageBox.Show("Test");
}

But I agree with you that the above is a sign of bad design.
 
R

Registered User

I need to use two namespaces that unfortunately has the same sub-namespace.

using System.Windows.Forms
using laserengineLib;

the laserengineLib contains a System namespace.

Now when I try to use a MessageBox.Show(msg); I get this error:

Error 1 The name 'MessageBox' does not exist in the current context


I then try with
System.Windows.Form.MessageBox.Show(msg);

and I get this error:

Error 2 'laserengineLib.System' does not contain a definition for 'Windows'

How can I specify that I want to use the standard System.Windows and not
the one in laserengineLib?

How to: Use the Namespace Alias Qualifier (C# Programming Guide)
<http://msdn.microsoft.com/en-us/library/c3ay4x3d(v=VS.90).aspx>

regards
A.G.
 
J

Jeff Johnson

using System.Windows.Forms
using laserengineLib;

the laserengineLib contains a System namespace.

And there's your problem!

I just have to echo what the others are saying: HORRIBLE design.
 
A

Anders Eriksson

And there's your problem!

I just have to echo what the others are saying: HORRIBLE design.

OK, this is an ActiveX module and the creator (not me) has written it in
C++. Don't thing he has any knowledge of .NET

In the end I have to remove

using laserengineLib;

and specify the whole "path" e.g. laserengineLib.LaserDoc

I will talk with the developer and suggest that he renames his class
System to something else...

PS! I could not get global:: to work. It still selected the system in
laserengineLib.

// Anders
 
A

Anders Eriksson

As I wrote, the solutions you attempted would work if the problem was
truly as you described. There's something else going on that you haven't
told us about.

You are probably right. I tried to create a sample program and when I
did it worked flawless! I then tried to change my real code to the
starting point and now it also works. I realize that I have done
something different but yet I don't what...
Since you apparently have access to the developer that got you into this
mess, your plan to talk to him to get him to fix it is probably the best
one. But you may find it useful to learn what's _really_ going on here
(i.e. it's not just a simple namespace conflict), so that if it comes up
in the future, you're better prepared to deal with it.
Yes I have talked to the developers and they will see what they can do.
It's late in the development...
Of course, no one here can help you with that until you've fully
described the problem. But once you have, I and others would be more
than happy to assist.

I would like to thank you Pete and all the others that give their
valuable time to help others.

I will try to understand what I have done and why it didn't work at first.

Thank you!

// Anders
 
J

Jeff Johnson

I will talk with the developer and suggest that he renames his class
System to something else...

Okay, I'm confused. Is System a class or a namespace???
 
A

Anders Eriksson

Okay, I'm confused. Is System a class or a namespace???

I did not make a difference between a sub-namespace and a class. But
fact is that the namespace is laserengineLib and it contains a class
called System.

Somehow this System and the namespace System got mixed up and the
compiler was looking for all the classes/namespaces that are under
System in the laserengineLib namespace.

I then did something, don't know what, and now the compiler finds
everything...

// Anders
 
A

Arne Vajhøj

I did not make a difference between a sub-namespace and a class. But
fact is that the namespace is laserengineLib and it contains a class
called System.

Somehow this System and the namespace System got mixed up and the
compiler was looking for all the classes/namespaces that are under
System in the laserengineLib namespace.

I then did something, don't know what, and now the compiler finds
everything...

Something must have been going on, because the described
structure is perfectly OK and should not cause any problems.

Arne
 

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