简体   繁体   中英

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.

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.

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!

在我的例子中,我在代码中间的一个方法的末尾遗漏了一个} ,这导致程序看不到其余的代码并抱怨我在那之后定义的方法。

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.

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.

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

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

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

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

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.

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

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

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM