Class cannot find another class in the same namespace
Asked Answered
A

5

7

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.

Acaulescent answered 5/11, 2015 at 19:20 Comment(16)
Are both classes in the same assembly?Supernal
Avoid using My. It will conflict with the built-in My namespace for anyone using VB that may want to use your assembly.Plated
Yes, they are both in the core WebApp project, not in external DLL projects. The namespace is just an example. I changed the name of it because the namespace setup by whoever first created the project includes the client name. I changed the example code to remove that ambiguity.Acaulescent
Is the General class syntactically correct?Agrestic
Beats me... Try a Clean and then a Rebuild.Supernal
The General class has no red squiggles and is not reporting any errors at build time. As far as I can see it is correct.Acaulescent
Are you running VS2015? I had this problem with 2015 and it drove me nuts. It worked when I closed VS and reopened. I have no clue as it stilll does this from time to time.Knurl
@MihaiCaracostea - nope, no luck with the C&R.Acaulescent
Try manually deleting the bin and obj folders from your project directories within the solution. (All project directories.) Then try another clean and build.Lockup
@Knurl - yeah, VS 2015. Just tried that... no joy.Acaulescent
Can you copy output of a verbose build?Mayenne
Is that the only compiler error you get?Spunk
@DStanley - yup; that is the lone and only error.Acaulescent
Try copying the class into the same file as PageBase, it may be that you accidentally set the settings for the General cs file to be something other than "Compile", can you check the file properties, specifically the Build Action?Counterintelligence
@Ron might be on to something, give it a shot. Make sure the Build Action is set to Compile for the General class's cs file.Supernal
@RonBeyer - BAM! That was it! What's weird is I literally just added this file as a new Class file from the "Add New Item" dialog, but somehow, the build action was set to Content. If you want to post that as an answer, I will accept it. I just assumed VS would add a new code file as Compile. LOL Silly me!Acaulescent
C
12

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.

Counterintelligence answered 5/11, 2015 at 19:49 Comment(5)
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.Quinidine
If the file is set to "Compile" you might just have to restart visual studio entirly. This bug seems to have stuck around with VS2017Wiggle
Confirmed, closing VS17 down, reopening, Clean, Rebuild seems to solve it.Altarpiece
As ecnepsnai said... Restart fixed it. ThxDarmstadt
still a prob in VS2017. I changed the problematic file from Compile==>to Content==>then back to Compile and it suddenly "found" it again.Merylmes
M
5

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.

Mycosis answered 2/4, 2021 at 13:25 Comment(0)
J
2

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

Jowett answered 27/9, 2021 at 20:49 Comment(1)
Unbelievable, we are in 2024 and still same issue and solution.Speech
H
0

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.

Hartung answered 30/4, 2019 at 20:8 Comment(0)
N
0

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

Naples answered 25/4, 2022 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.