'Class cannot find another class in the same namespace

I have a C# WebApp that we are doing for a client. I have two classes in the project, defined (in separate files) as such...

A general utility library:

namespace Client.WebApp {
    public static class General {
        public static MyDbClass GetDB() {
            //<creates and returns MyDbClass here>
        }
    }
}

A PageBase class:

namespace Client.WebApp {
    public class PageBase : System.Web.UI.Page {
        protected MyDbClass dbo { get; set; }

        public PageBase() {
            dbo = General.GetDB();
        }
    }
}

When I try to compile, I get the error from the dbo = General.GetDB(); line:
The name 'General' does not exist in the current context Client.WebApp.

  • I have double-checked that the namespace is spelled right in both files.
  • I have tried adding the top-level namespace Client.WebApp as a using directive in the pagebase file.
  • I have tried fully qualifying the name as in dbo = Client.WebApp.General.GetDB();

Nothing has helped and it insists that my General class does not exist. Funny thing is that while the build hits this error and stops, the code-editor does not see this error, and the statement does not get red-under-squiggled.

The project is targeting Framework v.4.5.2, and I am working in VS 2015.



Solution 1:[1]

It is a strange error, in my VS2015 if I set a file Build Action to anything other than "Compile", I get an error underline on any type for that file.

Anyway the solution here is to verify that the Build Action is set to "Compile", I'm not sure why adding a new file would have set the build action to anything other than "Compile". I've also tested trying to add new files in multiple ways (select a text file template and just name it something.cs), it still sets it as "Compile". You should verify that your VS2015 instance is updated with the latest updates.

Solution 2:[2]

I had the same problem: "type or namespace could not be found". It turned out that class was in a file with the "Build Action" property set to "None", meaning the file was not being compiled. I set it to "C# Compiler", that solved it.

Solution 3:[3]

I think I might have figure it out that if we create a file in a folder which suppose to have content files it set that file's build type content automatically. In my Case I create a file under app_code folder and it is a class file but the build type is set to Content which make it unavailable for any other classes i have.

May be it has any other reasons too.

Solution 4:[4]

This happens to me sometimes. Closing visual studio and opening it again always fixes it for me.

Solution 5:[5]

Try to create a new Class in the Project folder and then move it to the folder, you want it to be.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ron Beyer
Solution 2 Ed_75
Solution 3 jazz b
Solution 4 Observer
Solution 5 Sergey Frolov