Microsoft KB Archive/814907

= You receive the &quot;CS1513&quot; and &quot;CS1022&quot; error messages when you use the volatile modifier on local variables in Visual C# .NET or in Visual C# 2005 =

Article ID: 814907

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



SYMPTOMS
When you use the volatile modifier on local variables in Visual C# .NET or Visual C# 2005 code, you receive the following compiler error messages:

error CS1513: } expected

error CS1022: Type or namespace definition, or end-of-file expected

Note In Visual C# 2005, you receive the following compiler error message:

error CS0246: The type or namespace name 'STAThread' could not be found (are you missing a using directive or an assembly reference?



CAUSE
You cannot use the volatile modifier on local variables or on function variables. Use the volatile modifier only with class variables.



STATUS
This behavior is by design.



WORKAROUND
To work around this behavior, declare the volatile variables at class scope as follows: class test {   volatile int x = 0;

[System.STAThread] static void Main {    } }



Steps to Reproduce the Behavior
 Click Start, and then point to All Programs. Point to Microsoft Visual Studio .NET, point to Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt. This starts a console with the environment set to use the Visual Studio .NET command-line build tools.

Note In Visual Studio 2005, point to Microsoft Visual Studio 2005, point to Visual Studio 2005 Tools, and then click Visual Studio 2005 Command Prompt.  Start Notepad, and then paste the following code: class Test {      [STAThread] static void Main {               volatile int x = 0; } }  Save the file as volatile.cs   Use the following command from the Visual Studio .NET 2003 command prompt or the Visual Studio 2005 command prompt to compile the code: csc volatile.cs </ol>

You receive the compiler error messages that are described in the &quot;Symptoms&quot; section of this article.

Keywords: kbcompiler kbprb KB814907

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.