C# – File imported into App_Code isn’t referenced

asp.netassembliescnet

Q1

A) I’ve created a new .cs file, declared a class inside it and then save it on my disc. Now according to my book, I should be able to import this file into App_ Code folder of a website application ( via Website –> Add Existing Item. BTW – since file was copied into root folder of a website, I had to move it into App_Code folder manually), and the file would get automatically compiled and referenced by all pages in a website. But that is not the case, since when I try to use this class inside aspx page, I get compile time error saying that “Type or Namespace could not be found”

Since author probably knows his stuff, it’s safe to assume that I’m doing something wrong

B) If you build class library and want to reference this component from some web application, you can accomplish this via Website –> Add Reference. But when you build class library, VS creates two dlls, one located in Bin folder and other inside obj/debug folder. I assume I should add reference to dll which is contained inside Bin and not to dll contained inside obj/debug? If so, why?

thanx

EDIT:

I haven't left out any information, so hopefully the following info will be suficcient:

I've first created a file named Class1.cs, put the following code in that file, and saved it somewhere on my disk. Here is the code of Class1.cs:

using System;

namespace class1
{
     public class Class1
     {
           public static string someString = "this is Class1";
     }
}

I then copied Class1.cs into App_Code folder of my Web site. This Web site only has one aspx page and its code behind file. Anyways, after copying Class1.cs into App_Code folder, I tried to reference it from a code behind file:

using class1;

public partial class _Default : System.Web.UI.Page    
{
     protected void Page_Load(object sender, EventArgs e)
     {
           Label1.Text = Class1.someString; //this won’t compile
     }
}

Best Answer

For the first question we're going to need a bit more context. Can you post the class definition or at least the header? And also the place where it's referenced?

For the second question, use the one outside of bin\Debug. The obj folder is mainly used by the compiler and compilation system for temporary files used as part of the compilation process. These files in many ways are temporary and can be deleted by many operations. It's much better to rely on the files from bin\debug.