Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The name '__o' does not exist in the current context

I just installed Visual Studio 2015 and opened my asp .net project that I was working on. I'm receiving many errors (all exactly the same) as below:

Error CS0103 The name '__o' does not exist in the current context

Well actually I don't have any variables named __o and the code works like a charm (error is invalid) but what bothers me is that I'm not able to see when my code really has an error as it goes somewhere in this list and I should check the whole list.

enter image description here

like image 461
Ashkan Mobayen Khiabani Avatar asked Aug 07 '15 20:08

Ashkan Mobayen Khiabani


People also ask

Does not exist in the current context means?

Many a times while writing a code we get this error which says, “The name does not exists in the current context”. This is because the code behind file is not able to find the control being added to your aspx file.

How do I fix CS0103 error?

CS0103 is caused when you are using a name for a variable or method that does not exist within the context that you are using it. In order to fix the CS0103 error you will need to correct the name of the variable or method from where it is declared or referenced.


2 Answers

I found out that if I choose Build Only instead of Build + IntelliSense the errors (that are related to IntelliSense) will go away.

enter image description here

Update 1: The Reason

The reason that this is happening is that for codes like this:

<% if (true) { %>     <%=1%> <% } %> <%=2%> 

In order to provide IntelliSense in <%= %> blocks at design time, ASP.NET generates assignment to a temporary __o variable and language (VB or C#) then provide the IntelliSense for the variable. That is done when page compiler sees the first <%= ... %> block. But here, the block is inside the if, so after the if closes, the variable goes out of scope. We end up generating something like this:

if (true) {      object @__o;     @__o = 1; } @__o = 2; 

The workaround is to add a dummy expression early in the page. E.g.

<%=""%> 

This will not render anything, and it will make sure that __o is declared top level in the Render method, before any potential if (or other scoping) statement.

Update 2: Getting rid of this error without losing other IntelliSense errors

Click on the filter button on the top left corner of the error list panel and uncheck the CS0103 which the error code for the: The name '__o' does not exist in the current context and these errors will not be shown anymore and you can still have other IntelliSense errors and warnings:

enter image description here

like image 198
Ashkan Mobayen Khiabani Avatar answered Sep 20 '22 08:09

Ashkan Mobayen Khiabani


After reading the links given in the comments above, it turns out to be how intellisense handles if blocks.

Mikhail Arkhipov posted an explanation and workaround in the ASP.NET forums:

We have finally obtained reliable repro and identified the underlying issue. A trivial repro looks like this:

<% if (true) { %>     <%=1%> <% } %> <%=2%> 

In order to provide intellisense in <%= %> blocks at design time, ASP.NET generates assignment to a temporary __o variable and language (VB or C#) then provide the intellisense for the variable. That is done when page compiler sees the first <%= ... %> block. But here, the block is inside the if, so after the if closes, the variable goes out of scope. We end up generating something like this:

if (true) {      object @__o;     @__o = 1; } @__o = 2; 

The workaround is to add a dummy expression early in the page. E.g. <%="" %>. This will not render anything, and it will make sure that __o is declared top level in the Render method, before any potential if (or other scoping) statement.

Noting above, Failure's answer doesn't actually do much harm, other than hiding all intellisense error, which would be known anyway at build time.

Reference: http://youku.io/questions/324366/asp-net-mvc-error-name-o-is-not-declared https://msdn.microsoft.com/en-us/library/t8zbaa6f.aspx

like image 22
Syakur Rahman Avatar answered Sep 18 '22 08:09

Syakur Rahman