1

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

Mike
  • 11
  • 5

2 Answers2

1

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.

Dragon
  • 11
  • 3
  • OK, then another question. Looking at my code behind file there is no namespace declaration. So, does a program need one to work. The program does work, and it is a web site running on an IIS 7 server. So, do I just need to add a namespace if I want to access the classes I create? – Mike Apr 20 '18 at 03:19
  • It tells me the using statement is unnecessary. – Mike Apr 25 '18 at 03:11
  • If the code in both files is in the same namespace, it doesn't need the using statement. The code will work exactly the same event if the using statement is unnecessary. – Dragon Apr 26 '18 at 17:01
  • I may not know how to use, or add namespaces in my project. If this is true, then not sure if it's ignorance of Visual Studio, C#, or asp.net. I tried adding a namespace statement to all the files but still same problem. I am mobile right now, so next time I can get my development laptop connected to WiFi I will upload a sample of how I am trying to use the namespace. In the meantime, any suggestions applicable to some configuration needed to get Visual Studio or my project to actually use the namespace? – Mike Apr 27 '18 at 17:37
-1

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();
Jam Eel Ahmed
  • 79
  • 1
  • 10
  • To answer Mason's question about the type of project, I believe it is a website project. Actually not sure what the difference is. But to guess, a web application would be more like an intranet program that uses a web browser as it's interface. It does not negotiate a connection or do hand shaking. It just works on a PLAN. – Mike Apr 20 '18 at 03:17
  • That's a typo. I meant LAN. – Mike Apr 20 '18 at 03:23
  • @Mike no, that's completely wrong. A website project is not compiled into DLLs before deploying and has no csproj file. A web application project is compiled to DLLs and has a csproj file. It has nothing to do with handshaking or Internet vs LAN. Both are accessed through a web browser. – mason Apr 25 '18 at 04:02