views:

323

answers:

3

I sorta feel embarrassed because I am sure the answer to this is simple. Just to add some background, I am a full time java developer with a bachelors degree and 2 years of experience. I just started C# today using Visual Studio 2010 and Google to fill in the differences between C# and java.

So I had a console application that runs all good. All the code was in the main method, because I was trying to learn syntax and make sure things were working before I got too complicated.

I went to move all the code out of the main method and into its own class.

The main method and the class have the same namespace, but defined in separate files. One called Program.cs, the other called FileCache.cs.

FileCache.cs:

namespace SyncServer {
    public class FileCache {

        public FileCache() {
             //...code and such
        }

       //...methods and such
     }  
}

Program.cs:

namespace SyncServer {
    class Program {

        static void Main(string[] args) { 
            FileCache fileCache = new FileCache(); //major jitterbugs;
        }
    }
}

The type or namespace name 'FileCache' could not be found (are you missing a using directive or an assembly reference?)

I get no other compile errors or warnings. Also, if I move the FileCache definition into Program.cs, it compiles fine. I was hoping to find a solution where I didn't have to put ever class in the same file. :)

Thanks in advance!

+1  A: 

Does it compile if you replace FileCache with SyncServer.FileCache?

If so, you need to add a using directive:

using SyncServer;
Anon.
I have tried both qualify the class and adding the using in, and it gives the same error. I was under the assumption that as long as both pieces of code are in the same namespace that I wouldn't need to qualify it?
Anthony
@unknown that is correct
Rex M
Thanks for your help.
Anthony
+1  A: 

Try this:

  • Move your FileCache code back into your program.cs file
  • Delete the FileCache.cs source file
  • Right click the project in the Solution Explorer, choose Add->Class Note: If you don't see the Add sub-menu with a Class option, you aren't right clicking in the right place.
  • Name the new file FileCache.cs
  • Paste your code back in, cleanup as necessary
  • Cross 'em if ya got 'em and try giving it a compile

Deeper Note: The reason for going through this song and dance is to ensure that the "Project" knows about your source file and can compile/link/whatever it as necessary.

Goyuix
That worked. I guess it wasn't actually compiling the FileCache.cs... I am not sure why, but I guess from now on I will have to add new classes through the solution explorer (which I just learned about).Thanks!
Anthony
+1  A: 

I was facing the same problem.

Another simple solution:

In solution explorer, - Right click on FileCache. - Click Include in Project.

and you are done.

Babban Shikaari