views:

939

answers:

8

I have a couple of classes that I have created for my web project and I am going to store them in the App_Code folder. I don't want to store all the classes in the top level (no namespace) but want to organize them into namespaces. So far this does not seem possible because when I use the "using XXXX;" namespace statement it still can't find the right class in my code behind files. What could I be doing incorrectly?

EDIT:

So for example I have

namespace foo
{
    public class bar
    {
    }
}

Then in my code behind page for default.aspx I have

.
.
.
using foo

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        bar NewBar = new bar();
    }
}

The above DOES NOT work for me I have to use

foo.bar NewBar = new foo.bar();

Does anyone have any idea what I am doing wrong?

EDIT: Okay, so I have installed Visual Studio 2005 SP 1 and tried to convert it to a Web application and not a web site. STILL no luck.

A: 

Have you tried building the project first of all and then try "using" them ?

cyberbobcat
I wasn't really aware that they had to built first. For other classes without any namespace they are usable straight away. It is only when I try and organize them into namespaces.
uriDium
+3  A: 

The easiest solution would be to put the classes in a Class Library and then reference it from your web site project.

AdamRalph
Not correct. I have a web project that has namespaced classes in the app_code folder.
ctrlalt3nd
That's absolutely not the case at all.
Chris Pebble
I can get to the class BUT when i put using xxxx; then in my class i have something likexxxx newxxxx = new xxxx();that doesn't work. I still have to prepend the namespace to it to get it work like soxxxx.xxxx newxxxx = new xxxx(); that works.
uriDium
Sorry, my mistake. Answer edited accordingly.
AdamRalph
+1  A: 

have you put

namespace blah
{
   ...
}

in the code files?

ctrlalt3nd
A: 

You are using a Web Site Project, rather than a Web Application Project. Web Site Projects compile all the .cs files (including those in App_Code) on the fly, so that you can update them on the web server, without recompiling them. They are compiled into temporary dlls, which, under normal circumstances, will not be visible to you (ie, nothing in the bin). You have very little control of namespaces when writing Web Site Projects, as you have discovered. If you wish better structured code and more control over namespaces, then convert it to a Web Application Project. Your server-side code will be compiled into a dll and placed in the bin folder.

darasd
A: 

What are the names of the namespaces and classes? And what version of .net/visual studio?

ctrlalt3nd
See the edit above. Visual studio 2005 .net 2
uriDium
+2  A: 

(I'm guessing here) Try opening the file's properties and setting the build action to "compile".

ctrlalt3nd
This solution worked for me.
regex
Yea, this one worked for me as well..
miccet
A: 

I just tried your exact code sample in VS2005 and it works as expected with the using directive, no need to build the site first or anything.

That tells me that there's something else wrong with your project and/or namespaces. Will it build if you right-click and choose "Build Web Site"?

When you say it DOES NOT WORK, in what way exactly does it not work? Does the site not build? No intellisense completion? Error message when you browse to the page?

Mike Powell
I can't test right now. I have just started installing VS 2005 sp1 to get web application support as a few posters suggested. When I day it doesn't work I mean the intellisense brings up first the namespace then the class. Despite the fact that i have using. If i force compile => compiler error
uriDium
A: 

Hi guys

I have figured it out. I am really stupid. Sorry for the worry. It was because I had the namespace and the class name the same so it could not differentiate and needed the fully qualified name. As soon as I changed the class name everything worked as expected.

uriDium