7

I created a solution called Foo. Added a class library called Foo.Common Added a console app to call the library code from called ConsoleApp.

I referenced the Foo.Common from ConsoleApp and typed :

using Foo.Common;
public class Program
{
    CommonClass c = new CommonClass();            

    static void Main(string[] args)
    {
    }
}

and get this back :

Error 1 The type or namespace name '**Foo**' could not be found (are you missing a using directive or an assembly reference?) Z:\Foo\Solution1\ConsoleApplication1\Program.cs 3 11 ConsoleApplication1

Why am i getting this?

what s going on?

Jalal Said
  • 15,906
  • 7
  • 45
  • 68
Foo Bar
  • 141
  • 1
  • 1
  • 9
  • Is CommonClass declared as `public`? If not, you cannot use it outside the Foo.Common class library. – dtb Jul 15 '11 at 02:47
  • Are you using Visual Studio? If so, the Object Browser (View menu) will show you the loaded namespaces. – Hand-E-Food Jul 15 '11 at 02:59
  • It s showing Foo.Common. But the compiler is looking for Foo namespace. WTF? – Foo Bar Jul 15 '11 at 03:03
  • Move the using statement out of the namespace declaration? – mletterle Jul 15 '11 at 03:07
  • If you have a `using Foo.Bar.Baz;` directive and the compiler cannot find `Foo.Bar.Baz`, it looks for `Foo.Bar`. And if it cannot find that, it looks for `Foo`. If it cannot find that as well, it says that `Foo` could not be found, even though it was originally looking for `Foo.Bar.Baz`. – dtb Jul 15 '11 at 03:08

6 Answers6

6

Make sure that

  • The ConsoleApp project has a reference to the Foo.Common project (do not browse for Foo.Common.dll),

    Screenshot

  • the file contains a using directive for the namespace in which CommonClass is declared, and

  • CommonClass is declared as public.

So your files should look like this:


CommonClass.cs in Foo.Common project:

namespace Foo.Common
{
    public class CommonClass
    {
        public CommonClass()
        {
        }
    }
}

Program.cs in ConsoleApp project:

using Foo.Common;

namespace ConsoleApp
{
    public class Program
    {
        public static void Main()
        {
            CommonClass x = new CommonClass();
        }
    }
}
dtb
  • 213,145
  • 36
  • 401
  • 431
2

Ensure that under your project settings, the target framework is set as .NET Framework 4 and not .NET Framework 4 Client Profile. I got this same behavior when it was set to Client Profile and it was fixes as soon as I set it to just the regular .NET Framework 4.

hspain
  • 17,528
  • 5
  • 19
  • 31
0

It looks like Foo Bar got this error because his project's target framework was set to the client profile.

Just thought I'd add one more 'solution' -- I created a library that targeted the 4.5 framework. My older project was tarting the 4 framework. I got this error.

Changing the older project to 4.5 made it work.

Gerard ONeill
  • 3,914
  • 39
  • 25
0

Right Click on the new console app solution/project and Add Reference and add the project that contains the Foo namespace

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
0

Did you add a reference to the library? Look under "References" in the console project. If its not there, you need to add it.

0

I posted this as a comment, but I want to expand on it here. What's probably happening is it's seeing using as a statement and not a keyword. It appears you have something like the following:

using System;
namespace TestNamespace
{
  using Foo.Common;
  public Class { }
}

Try

using System;
using Foo.Common;
namespace TestNamespace
{
  public Class { } 
}

Instead.

mletterle
  • 3,968
  • 1
  • 24
  • 24
  • 1
    It is perfectly valid to have a `using` directive inside a namespace declaration. There is no ambiguity with a `using` statement. – dtb Jul 15 '11 at 03:15
  • 1
    Indeed, not only is it valid, but some people strongly recommend it, [this question](http://stackoverflow.com/q/125319/180803) has some good information. Although, having one `using` inside the namespace and one outside is surely not good style! – Carson63000 Jul 15 '11 at 05:25
  • Yeah, I realized that shortly after I posted, it's really useful for doing namespace aliasing. Still, I thought it might be related, guess it was the .net 4 client profile thing though. – mletterle Jul 15 '11 at 13:25