Class cannot find another class in the same namespace

10,896

Solution 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

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.

Share:
10,896
eidylon
Author by

eidylon

Software developer working primarily in VB.NET, C# and Sql Server. Occasionally working in ORACLE. I love RegEx and jQuery, and have just started playing around with WebAPI. Hobbyist prop maker. Have created several Mass Effect replica weapons as well as a number of custom-designed commission pieces. Projects and pictures on my Facebook page... https://www.facebook.com/darkchannelprops

Updated on June 24, 2022

Comments

  • eidylon
    eidylon almost 2 years

    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.

  • DWright
    DWright almost 8 years
    Man, you just helped me figure out something that was driving me crazy! For some reason one file had gotten set to Build Action = Content, not Compile. Super non-obvious. Finding your answer made me check.
  • ecnepsnai
    ecnepsnai over 5 years
    If the file is set to "Compile" you might just have to restart visual studio entirly. This bug seems to have stuck around with VS2017
  • Bryan Harrington
    Bryan Harrington about 5 years
    Confirmed, closing VS17 down, reopening, Clean, Rebuild seems to solve it.
  • Jeff Blumenthal
    Jeff Blumenthal about 3 years
    As ecnepsnai said... Restart fixed it. Thx