Bug with var keyword and .NET 2.0 websites

I stumbled across this bug today while editing a .NET 2.0 ASP.NET web application project in Visual Studio 2008 and it may actually pertain more to Visual Studio than to ReSharper itself so I've submitted the bug report there as well...

In a .NET 2.0 ASPX markup file I was doing some C# logic within the page itself and at the suggestion of ReSharper's code inspection I switched an explicit variable definition to the 'var' keyword inside an ASP script tag (<% %> or <script></script>). After doing this I ran the page only to receive a compilation error stating that the 'var' keyword was unrecognized despite Visual Studio properly highlighting it. Testing further I moved the same expression into the code behind, compiled, and ran the page without incident. Reproducing the same steps in a .NET 3.5 web application produces no compilation error.

Suggested fix: Don't offer the code inspection 'var' keyword suggestion inside an ASPX markup file if the current project is built on .NET 2.0.


Code:


<%

var x = "Hello World";

Response.Write(x);

%>

Expected Result:


Hello World

Actual Result:


Server Error in '/' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

2 comments

Select the web site project and hit F4 (Properties - NOT the Property Pages
link from the context menu!). Under the Resharper section there is an entry
for Framework version. Select 2.0 and you will not get the suggestion for
"var", or any other 3.0+ construct.

HTH

"Nathan Taylor" <no_reply@jetbrains.com> wrote in message
news:19022328.123741246480006997.JavaMail.clearspace@app8.labs.intellij.net...
>I stumbled across this bug today while editing a .NET 2.0 ASP.NET web
>application project in Visual Studio 2008 and it may actually pertain more
>to Visual Studio than to ReSharper itself so I've submitted the bug report
>there as well...
>

In a .NET 2.0 ASPX file I was doing some C# logic within the page itself
and at the suggestion of ReSharper's code inspection I switched an
explicit variable definition to the 'var' keyword inside an ASP script tag
(<% %> OR <script></script>). After doing this I ran the page only to
receive a compi
lation error stating that the 'var' keyword was unrecognized despite
Visual Studio properly highlighting it. Testing further I moved the same
expression into the code behind, compiled, and ran the page without
incident. Reproducing the same steps in a .NET 3.5 web application
produces no compilation
error.

>

Suggested fix: Don't offer the code inspection 'var' keyword suggestion
inside an ASPX markup file if the current project is built on .NET 2.0 as
the page will not execute correctly.

>

Code:

>

<%

>

var x = "Hello World";
Response.Write(x);

>

%>

>

Expected Result:

>

Hello World

>

Actual Result:

>

Server Error in '/' Application.

>

Compilation Error

>

Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.

>

Compiler Error Message: CS0246: The type or namespace name 'var' could not
be found (are you missing a using directive or an assembly reference?)

>

---
Original message URL:
http://www.jetbrains.net/devnet/message/5241097#5241097



0

Hello,

Indeed, there is an issue in ReSharper. Its "C# 3.0" feature policies have
been written for regular applications, in which the production environment
cannot "see" whether it were C# 3 or C# 2. You can use any of the C# 3 constructs,
as long as they have no influence on the output IL code.

This is different for the Web kind of projects because they have part of
their code compiled on the side of the server. As far as I remember, in this
case we cannot predict whether it will be compiled with csc 2.0 or csc 3.5,
depending on the server config. So it could easily be that some of the C#
code in your project is compiled with csc 3.5 (locally on your machine),
and some inline C# code is compiled with csc 2.0 at run time right at the
production environment.

Currently, R# does not support this mixed case. I've heard about some plans
to support it, though -- as it can really make trouble because the once-safe
code transformations are in no way safe anymore in such a setup.


Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”


0

Please sign in to leave a comment.