Partilhar via


'__o' is not declared - what does it mean?

A few customers complained about an error

Error   1    Name '__o' is not declared.   

that seems to show up and disappear for no particular reason whe editing ASP.NET Web pages. The page runs OK though. 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 when you type in the <%= %> block, similarly to what happens when you type x=.

The variable is declared on demand when page compiler sees the first <%= ... %> block.  But here, the block is inside the if, so after the if statement closes, the variable goes out of scope. We end up generating something like this:

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

Obviously, this causes compiler to complain that __o is not declared.

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 page, before any potential ‘if’ or other scoping statement.