namespace not found!

13,657

Solution 1

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();
        }
    }
}

Solution 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.

Share:
13,657
Foo Bar
Author by

Foo Bar

Updated on June 23, 2022

Comments

  • Foo Bar
    Foo Bar almost 2 years

    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?

  • Foo Bar
    Foo Bar almost 13 years
    I dont have a Foo namespace. I have Foo.Common
  • Foo Bar
    Foo Bar almost 13 years
    and i added Foo.Common library as a reference.
  • Jesus Ramos
    Jesus Ramos almost 13 years
    so it looks like namspace Foo.Common { public class CommonClass ... } in the other solution?
  • Jesus Ramos
    Jesus Ramos almost 13 years
    When you type does the autocomplete show Foo.Common? If anything try Foo.Common.CommonClass = new Foo.Common.CommonClass()
  • Foo Bar
    Foo Bar almost 13 years
    it s exactly like this. but when i compiled, i m getting compilation error as above.
  • Foo Bar
    Foo Bar almost 13 years
    That s exactly how I added the reference via Projects tab.
  • Foo Bar
    Foo Bar almost 13 years
    Ok the errro was due to Project was in .net 4 client profile in the settings.
  • Feidex
    Feidex almost 13 years
    It is perfectly valid to have a using directive inside a namespace declaration. There is no ambiguity with a using statement.
  • Carson63000
    Carson63000 almost 13 years
    Indeed, not only is it valid, but some people strongly recommend it, this question has some good information. Although, having one using inside the namespace and one outside is surely not good style!
  • mletterle
    mletterle almost 13 years
    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.
  • Samvel Siradeghyan
    Samvel Siradeghyan over 11 years
    @Foo Bar you last comment must be answer :)))
  • Matt
    Matt almost 11 years
    @FooBar please make your comment the answer :)