Microsoft KB Archive/323487

= PRB: JScript .NET Compiler Generates Unexpected Errors for Comments in an .aspx Page =

Article ID: 323487

Article Last Modified on 2/23/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft JScript .NET

-



This article was previously published under Q323487



SYMPTOMS
When server-side code in an .aspx page contains an at sign (@) immediately after a comment statement, the JScript .NET compiler may generate unexpected errors.

The following code demonstrates this scenario: Response.Write(&quot;working comment&quot;); // comment Response.Write(&quot;causes error&quot;); //@ comment2 Response.Write(&quot;causes another error&quot;); //@@@comment3  The first line represents a typical comment, which functions as expected. The JScript .NET compiler generates the following error message for the second line (which contains comment2):

0x800a046f: Variable 'comment2' has not been declared

 The JScript .NET compiler generates the following error message for the third line (which contains comment3):

0x800a03f6: Invalid character





CAUSE
This problem occurs in both scenarios because of the way that JScript .NET implements preprocessor directives. The .aspx page is transformed into a JScript class file with the preprocessor set to on so that JScript can remap line numbers. This means that the &quot;//@&quot; characters are significant.



RESOLUTION
To resolve this problem, make sure that you are aware of how JScript .NET implements preprocessor directives when you write code in an .aspx page.

To work around this problem, you can vary the commenting style a little. However, Microsoft recommends that you not start comment statements with the &quot;//@&quot; characters. Only use this format for preprocessor statements.

The code that follows demonstrates how to use a version of the code from the &quot;Symptoms&quot; section that does not cause any errors. Also, this code includes two forms of JScript .NET conditional compilation to illustrate how JScript .NET preprocessor statements can appear with different comment statement types. <%   //Modified version of one of the previous comment statement //sample. This variation does not cause the compiler error. // @comment2

//Example that uses conditional compilation to illustrate //preprocessor statements in JScript .NET. //@cc_on //@ var thisWorks : boolean = true; //@ if(thisWorks){ //@ Response.Write(&quot; This works! &quot;); //@ }   //Another variation /*@if(@_jscript_version >= 5){ Response.Write(&quot;JScript Version: &quot; + @_jscript_version); }     @end @*/ %>



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Follow these steps to add a new Web Form to your ASP.NET Web Application: <ol style="list-style-type: lower-alpha;"> Right-click the project node in Solution Explorer, point to Add, and then click Add Web Form.</li> In the Name box, type JScriptTest.aspx .</li> Click Open.</li></ol> </li> In the Properties window, set the language property for the page to JScript.</li>  Make sure that the HTML tab is selected in the editor, and then add the following server-side code render block to the .aspx page: <% Response.Write(&quot;working comment&quot;);      // comment Response.Write(&quot;causes error&quot;);        //@ comment2 Response.Write(&quot;causes another error&quot;); //@@@comment3 %>                   </li> On the File menu, click Save All to save the Web Form and other files that are associated with the project.</li> On the Build menu, click Build Solution.</li> Right-click the Web Form page, and then click View in Browser. Notice that you receive error messages in your browser.</li></ol>

<div class="references_section">