Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

namespace not found!

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?

like image 606
Foo Bar Avatar asked Jul 15 '11 02:07

Foo Bar


People also ask

How do I resolve CS0246 error?

There are two solutions to this error. The first is to correct the name of the namespace to match one that already exists. The second is to fix the custom namespace that was created.

What is a namespace error?

The error means the namespace is not found in the partition map (keyed by the specified namespace).

How do I add a namespace C#?

In Solution Explorer, double-click the My Project node for the project. In the Project Designer, click the References tab. In the Imported Namespaces list, select the check box for the namespace that you wish to add. In order to be imported, the namespace must be in a referenced component.


2 Answers

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();
        }
    }
}
like image 90
dtb Avatar answered Sep 29 '22 19:09

dtb


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.

like image 26
hspain Avatar answered Sep 29 '22 19:09

hspain