views:

92

answers:

2

Guys,

I know I am overlooking something really simple here.

I am using Visual Studio 2008 and created an ASP.NET 3.5 project and solution. I added another project to my solution, a class library. I added a reference to my class library. Now when I click on the properties of my class library, the Default Namespace is set to "HRCommon", which is correct.

Now for some reason, from my ASP.NET application, it's automatically importing the HRCommon namespace. So when I want to reference a class out of my class library, I just need to type the classname. I want to have to specify the whole name, like HRCommon.<ClassName> instead of just <ClassName>.

Anyone tell me what I'm overlooking here? The language is C#, btw.

+1  A: 

What namespace is your ASP .Net code in?

It's probably inside of HRCommon.

If you write code in a nested namespace, it will automatically import all parent namespaces (eg, code in System.Windows.Forms doesn't need to import System.Windows and System)

To prevent this, move either the library or the ASP .Net project to a different namespace.


Alternatively, you might not have namespace statements in the library.

The Default Namespace option in VS is only used to generate the namespace block in new C# source. EDIT: To clarify, the Default Namespace setting is not used by the compiler at all. It's only used by the Visual Studio to automatically insert namespace blocks in new source files.

Check the source files in the library and make sure that all of the classes are defined inside of the namespace HRCommon.

For example:

namespace HRCommon {
    public class MyClass {
        //...
    }
}
SLaks
My ASP.NET code isn't in a namespace, but its in a totally different project all together. I can't see how it would be in HRCommon namespace.
icemanind
Make sure that the classes are defined in a namespace.
SLaks
If it's an asp.net web application (as opposed to web site) then it definitely is in a namespace. You can right-click->properties on the web project to see.
Craig
I meant in the library.
SLaks
My library code isn't surrounded in a namespace block. I assumed that by setting the default namespace in the project properties, it would automatically do this. Isn't that what the default namespace is designed to do?
icemanind
No. As I explained, the Default Namespace setting is only used to generate the `namespace` block in new source files. (Unlike VB, which behaves the way you're thinking)
SLaks
No; only VB will do that for you.
SLaks
That's what it is. I'm a VB developer and C# is relativly new to me. lol thanks for your help SLaks. I went ahead and surround my code with namespace { } blocks and now its working as expected! Thanks!
icemanind
A: 

EDIT: The problem is that you didn't put the library code in a namespace (as described in the lower half of my first answer); I'm leaving this here for reference.

Check that HRCommon isn't in the list of default namespaces in Web.config. (It probably isn't, but no harm checking).

Specifically,look in system.web/pages/namespaces. If your Web.config doesn't have a namespaces element (the default one doesn't), this is obviously not the issue.

SLaks