C# ASP.net How to access new classes from code behind, not kept in APP_CODE, but kept in same folder as code behind

1.7k views Asked by At

I want to create classes that are separate files (not code within he aspx.cs) file, and I do not want to put them in the APP_CODE directory.

I want the separate class files to reside in the same directory as the main code behind file (aspx.cs).

Each class will perform a function.

I may be using these classes improperly so correct my terminology if I am wrong.

What I want is for many of my functions (methods) to be code in files outside my code behind file and simply called from withing the code behind file. I believe I do this by creating classes. Sorry if I am wrong I am quite a bit rusty.

Each class (or whatever it is called) will be named according to its function.

I am so ignorant of how to do this, I really need a tutorial. I cannot find any, or any other answers that address exactly what I want to do. I believe this is called an "outer class". Where as an "inner class" is written inside of another class.

I know how to write a class file, I just don't know how to access it from the code behind file aspx.cs

2

There are 2 answers

4
Dragon On

I don't think the file path matters, but you need to use the namespace right. If you have the Question class defined within the TestProject.Pages namespace in your project, like this:

namespace TestProject.Pages
{
    public class Question
    {
    }
}

Then you add this line to your aspx.cs file, which lets that code see the namespace you used above:

using TestProject.Pages

and you should be able to access the Question class.

3
Jam Eel Ahmed On

It's simple!

// you can access classes that are in different folders. by mention the folder in which class resides then put '.' then class name.

e.g :

myfolder.myclass cls = new myfolder.myclass();