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

44,049

Solution 1

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

Solution 2

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

Solution 3

Declare variable __o in code, like this:

public object __o;

Be sure to put it in Master page (if you are using one).

Solution 4

<% response.write(var) %> instead of <% =var %> removes the error without declaring __o as suggested other posts

http://forums.asp.net/p/923745/1266105.aspx

Solution 5

What resolved this problem from its heart for me was to add a dummy expression early in the page. E.g. <%="" %>. Check out this link for further explanation: https://forums.asp.net/post/1263727.aspx

Share:
44,049
Ashkan Mobayen Khiabani
Author by

Ashkan Mobayen Khiabani

I'm a Programmer and Web Developer from Tabriz Currently living in Istabul/Turkey.

Updated on January 30, 2022

Comments

  • Ashkan Mobayen Khiabani
    Ashkan Mobayen Khiabani over 2 years

    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

  • Dan
    Dan almost 9 years
    Having the same problem here. I could hide IntelliSense errors, but that seems like brushing it under the rug. Want to trace the problem.
  • Taurib
    Taurib over 8 years
    This is not the fix, like Dan said, that it is just hiding that problem. Plus it would hide all IntelliSense errors, not just that one..
  • kevinpo
    kevinpo almost 8 years
    You should put the part about using "<%="" %> at the top of your answer so it stands out.
  • needfulthing
    needfulthing over 4 years
    I don't understand why someone should -1 this answer. It is more elegant than adding <%="" %>.
  • Jay Imerman
    Jay Imerman over 4 years
    This was a temporary resolution, as it resolved it for a moment, then for some reason the error message came back. Now, even if i add this near the beginning of the page, it no longer resolves the message.
  • Andy
    Andy almost 4 years
    this one worked for me (visual studio 2019), .NET 4.8). I couldn't make <%="" %> work
  • blackorchid
    blackorchid about 2 years
    This dummy workaround (<%=""%>) is excellent, thanks!