Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio won't find class in separate .cs file

I have two classes in separate files in a project I'm working on. Windows forms classes specifically. When I try to use one class from the .cs file, Visual Studio cannot find it. It underlines the line of code and asks if I'm missing a directive or assembly reference. I have no idea why it isn't seeing the other class, as both .cs files are in the same directory. Does anyone have any ideas as to why this would be happening and how to fix it?

Also, one of the .cs files is copied from a separate project, so I don't know if that caused a problem somehow.

like image 375
grantmcconnaughey Avatar asked Mar 30 '13 21:03

grantmcconnaughey


2 Answers

Also, one of the .cs files is copied from a separate project, so I don't know if that caused a problem somehow.

You should check the namespaces involved. My guess is that the one from the other project is in a different namespace. So, if you've got:

namespace FirstNamespace
{
    class Foo
    {
        private Bar bar = null;
    }
}

and

namespace SecondNamespace
{
    class Bar 
    {
    }
}

Then in the first class you need:

using SecondNamespace;

to allow you to use Bar without any qualification.

Or - possibly better - you could just change them so they're both in the same namespace.

Better yet, you could avoid copying anything from a different project, and instead use class libraries for code sharing.

like image 120
Jon Skeet Avatar answered Nov 15 '22 07:11

Jon Skeet


In my case namespace was fixed but still visual studio did not recognized classes. I solve it by right click on the class file and click 'Include In Project'.

like image 21
Amir Avatar answered Nov 15 '22 07:11

Amir