views:

58

answers:

2

I'm working on an ASP.NET web application with .NET 3.5 and have run into the following problem:

I'm working with a class under the namespace X.Web.Controls.Core which references the class Utils in the namespace X.X2.components.util.

I get an error that Utils is already defined in the namespace X.Web.Controls.Utils This should not be possible since I can't find anything referencing that namespace from the class I'm working on. Any ideas?

+6  A: 

I can't really see that there should be a problem unless you have a using statement referencing it somewhere. Do take care that code in a namespace will implicitly "see" classes in the same namespace, even if they're defined elsewhere.

Anyways, you can solve your problem by changing the class name (for the current code file only):

using X2Utils = X.X2.components.util.Utils;

The class will be named X2Utils in your code. Alternatively you can make a shortcut for its namespace:

using X2util = X.X2.components.util;

Now you can refer to the class using X2util.Utils.

Blixt
Thanks for your reply. I actually have a few hundred classes that i have to go through and add aliases to. I think that taking into account the massive amount of classes, ill try renaming the Utils class into something that doesnt conflict with any namespace (like 'Util').
CodeSpeaker
You can also alias namespaces though, but I see your point. Yes, I agree that you should use a different name for your class. "Util" is a *very* broad name and not much more descriptive than "AClass"...
Blixt
+1  A: 

You're working in X.Web.Controls.Core which is a subnamespace of X.Web.Controls. That means the namespace Utils in X.Web.Controls is implicitly visible.

Solve using an alias as suggested by Blixt.

Per Erik Stendahl