views:

753

answers:

2

Hello,


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
     }
}
+1  A: 

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.

JaredPar
+1  A: 

For the 1st question, try right clicking on your App_Code folder and adding a new class from that menu. Then copy your code over from the old file. See how that goes.

For the 2nd question. It sound's like you already added a "project reference". When you do that, Visual Studio automatically copies the DLL at build time from the class librarys output folder (in this case obj/debug) into the web apps Bin folder. If you have added a project reference, you don't need to add another reference to the DLL directly.

Greg B
Hello,I tried adding it the way you suggested, but it still didn't workAbout your second answer - Question B is not related to question A
SourceC
If the DLL is in the Bin folder of the target project, it's usually because the project has a reference to the class library.
Greg B
I understand that. But with regards to question A, I don't have dll in the Bin folder of the target page, since Class1.cs is not a class library, but just a single .cs file.
SourceC