Blog Archives

Are your classes created under App_Code folder not recognized in other classes?

It is a very simple issue and i always forgot to fix it before i add any class to App_Code folder.

Problem : 

Let’s say you have a class under App_Code folder


namespace Enhancements.App_Code
{
public static class Defaults
{
public static readonly string PAYROLL_JOB = "Payroll_Test";
}
}

Now  you are trying to reference this Defaults class in some other class say webpage UI code behind class.


namespace Enhancements
{
   public partial class SqlJobManager : System.Web.UI.Page
   {
      private static string payrollJobName;

      protected void Page_Load(object sender, EventArgs e)
      {
          payrollJobName =<strong> Defaults.PAYROLL_JOB</strong>; // Defaults class is not recognized here.
      }
}

It happens due to the default build action (which is ‘Content‘) of the class added to the app_code folder.

Solution: Right click on the Class and select properties –> Advanced –> Build Action –> Select ‘Compile

Explaination:  According to MSDN documentation we can set build action for every class type we add to the solution

The BuildAction property indicates what Visual Studio does with a file when a build is executed. BuildAction can have one of several values:

None – The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.

Compile – The file is compiled into the build output. This setting is used for code files.

Content – The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.

Embedded Resource – This file is embedded in the main project build output as a DLL or executable. It is typically used for resource files.

Now we are good to go!

I hope it was useful.

Advertisements