namespace not found!
Solution 1
Make sure that
The ConsoleApp project has a reference to the Foo.Common project (do not browse for Foo.Common.dll),
the file contains a
using
directive for the namespace in whichCommonClass
is declared, andCommonClass
is declared aspublic
.
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.
Foo Bar
Updated on June 23, 2022Comments
-
Foo Bar almost 2 years
I created a solution called
Foo
. Added a class library calledFoo.Common
Added a console app to call the library code from calledConsoleApp
.I referenced the
Foo.Common
fromConsoleApp
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 almost 13 yearsI dont have a Foo namespace. I have Foo.Common
-
Foo Bar almost 13 yearsand i added Foo.Common library as a reference.
-
Jesus Ramos almost 13 yearsso it looks like namspace Foo.Common { public class CommonClass ... } in the other solution?
-
Jesus Ramos almost 13 yearsWhen you type does the autocomplete show Foo.Common? If anything try Foo.Common.CommonClass = new Foo.Common.CommonClass()
-
Foo Bar almost 13 yearsit s exactly like this. but when i compiled, i m getting compilation error as above.
-
Foo Bar almost 13 yearsThat s exactly how I added the reference via Projects tab.
-
Foo Bar almost 13 yearsOk the errro was due to Project was in .net 4 client profile in the settings.
-
Feidex almost 13 yearsIt is perfectly valid to have a
using
directive inside a namespace declaration. There is no ambiguity with ausing
statement. -
Carson63000 almost 13 yearsIndeed, 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 almost 13 yearsYeah, 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 over 11 years@Foo Bar you last comment must be answer :)))
-
Matt almost 11 years@FooBar please make your comment the answer :)