Jarrod Everett
Jarrod Everett

Reputation: 801

Visual Studio saying name doesn't exist in current context

I am calling a static method on a class like

Foo.bar()

Visual studio's intellisense recognizes Foo and autocompletes bar for me (it highlights Foo and everything like it is working fine). Everything looks fine until I go to build the project, and it throws an error saying the name Foo doesn't exist in current context.

I am using this static method call in other files, so I know the class is ok. The situation is too big to post code, so I am mostly looking for reasons to start looking into that would cause intellisense to function normally but get errors on compile like this.

Upvotes: 22

Views: 98103

Answers (17)

nick_b
nick_b

Reputation: 37

Per Donald's answer - debugging with target set to "Release" means some variable watches show this error.

Upvotes: 0

hooah
hooah

Reputation: 76

In my case, I was missing the following lines in my csproj file

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE</DefineConstants>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

Once I added this, I could see the variables while debugging

Upvotes: 0

Joe Gurria Celimendiz
Joe Gurria Celimendiz

Reputation: 490

Changing the id of the control resolved the issue for me. Apparently the id of the control existed in another part of the solution.

Upvotes: 0

Allan S. Hansen
Allan S. Hansen

Reputation: 4091

I know this is a bit old topic, but I just experienced the same and for me it was because the file was not actually included in the solution.

I properly happened because I had renamed the class and then the file, which caused Visual Studio to still know the class and the namespace, but the compiler did not get the file as the renamed file was not included.

Upvotes: 3

Ted
Ted

Reputation: 20184

My solution to this problem that occurs every now and then:

  1. Find the class that is giving you problems in the Solution Explorer and "Exclude From Project"
  2. Rebuild that assembly (let's call it "A")
  3. The project that used the file ("B") will ask you to "Reload" project, wait
  4. Add the file back into assembly A, that you just removed it from, and rebuild
  5. Now, reload project B

Then the file was found in VS and all was well.

Upvotes: 0

hasselmann
hasselmann

Reputation: 493

In my case I had to reload the project that was marked "missing".

  1. Project > Unload Project
  2. Project > Load Project
  3. Clean, Build Solution

Upvotes: 3

Tran Thanh Thai
Tran Thanh Thai

Reputation: 11

Adjust the related file. If the error code in Default.aspx.cs, you need to change the top line in the file Default.aspx as below:

Replace "CodeFile=" with "CodeBehind"

Hope this can help.

-Thanks, Thai_FUV

Upvotes: 1

yagni
yagni

Reputation: 1210

Mine was a little more convoluted solution. Project A referenced projects B and C: both references had Copy Local to true and both produced assemblies with identical names. When building the referencing project, the output assemblies from projects B and C were copied and one overwrote the other because they had the same name. VS was then looking for the references within the build directory and only found the assembly that had "won."

Upvotes: 0

chelder
chelder

Reputation: 3897

Closing all tabs of MonoDevelop. Then Closing MonoDevelop. Finally opening MonoDevelop again solved the problem for me.

Upvotes: 0

William Biesty
William Biesty

Reputation: 11

I also was running into this issue creating a data access layer and had static methods being called with the same symptoms: Intellisense finding it but not the compiler. I tried many of the above, including fixing the .Net version.

When adding the source files to the project I also changed the namespace. With the file with the issue, I forgot to change the namespace to match when it was imported at another time.

Upvotes: 0

SimperT
SimperT

Reputation: 2867

I have run into this probelm a few times and so when I do, the first thing I check is if the assembly not recognized has any Nuget packages. In my cases they always have and I simply forgot to install the same packages in the assembly of which the reference to the un-recognized assembly is in. A re-build command and problem fixed. I hope this helps someone. This same error message can be given for multiple things so this particular case, may not apply. If you have not used Nuget than I would suggest trying the other answers

Upvotes: 0

Donald McInnes
Donald McInnes

Reputation: 81

Old thread I know, but I've encountered this issue when referencing a static method from within a unit test project - intellisense said the method was there, but when I tried to build/run the test (in Debug mode) I got the error 'name doesn't exist in current context'. In order to fix it I had to rebuild the project containing the referenced static method in Debug configuration (it had only previously been built in Release configuration) - after this the test built and ran OK.

Upvotes: 5

user1012598
user1012598

Reputation: 427

this is an old article I know, but I just encountered this issue and has been puzzling me for couple of days, and eventually got to it: click on the class file, in Solution Explorer, then look at the Properties tab; make sure Build Action is set to "Compile".

Upvotes: 1

aherocalledFrog
aherocalledFrog

Reputation: 861

I've seen this error caused by differing versions of the .NET framework in the different projects. The Class Library I built was 4.5 and the application was 4.0, but the only error it gave was namespace errors. Changing the framework version on the class library and rebuilding it, then the application, resolved the error.

Upvotes: 34

Mehrad
Mehrad

Reputation: 4203

In my case I was missing a } at the end of one of the methods in the middle of the code which was causing the program not see the rest of the code and complain about the Methods I have defined after that point.

Upvotes: 7

Michael
Michael

Reputation: 11

Consider doing a Clean and then a Build on the project with the problem. It is possible for the editor and Intellisense to correctly discover the class, while the compiler works with files that are out-of-date. (I had this same problem, and that's how I resolved it.)

Upvotes: 1

Matthew Layton
Matthew Layton

Reputation: 42390

This can occur when namespaces, classes and variables become tangled when they have the same name. I have suffered with this before. Intellisense told me I was right, the compiler told me I was wrong! I trusted the compiler!

You have 2 options that I can think of

  1. Search your code for Foo, and see it it is being used for something other than the static class.

  2. Fully qualify the Foo.bar() call. MyApplication.This.That.Foo.bar();

Do it in that order...it's better to elegantly resolve the issue so you can just call Foo.bar() as this is more readable and maintainable than having MyApplication.This.That.Foo.bar(); all over the place!

Upvotes: 9

Related Questions