BUG: You may receive a "CS0500" compilation error when you compile code that is generated in the Method Wizard in Visual C# .NET (811599)



The information in this article applies to:

  • Microsoft Visual C# .NET (2002)

SYMPTOMS

When you use the Visual C# .NET Method Wizard to add an abstract method that returns a value, you may receive the following compilation error:
error CS0500: '<your fully qualified method name>' cannot declare a body because it is marked abstract

CAUSE

The Visual C# .NET Method Wizard generates code for the method body for abstract methods that return a value. If a method is marked as abstract, the Visual C# .NET Method Wizard sets the MustImplement property to true. Therefore, when the wizard generates code, if the abstract method returns a value, the wizard generates a method body. If the abstract method has a void return type, the wizard does not generate a method body.

RESOLUTION

To resolve this problem, delete the generated method body code from the abstract method. For example, if you use the Visual C# .NET Method Wizard to generate an abstract method that returns an "int", the wizard generates the following:
public abstract int myAbstractMethod()
{
   return 0;
}
Remove the code that is generated, and then add a semi-colon at the end of the function definition. The modified code appears as follows:
public abstract int myAbstractMethod();

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Visual Studio .NET. On the File menu, point to New, and then click Project.
  2. Under Project Types, click Visual C# Projects, and then click Console Application under Templates.
  3. Type SampleApplication as the project name, and then click OK.
  4. To add an abstract class, follow these steps:
    1. In the Class View, right-click SampleApplication, point to Add and then click Add Class.
    2. In the Class Wizard, follow these steps:
      1. Type MySampleClass as the class name.
      2. Click to select Abstract under Class modifiers section.
      3. Click Finish.
  5. To add an abstract method, follow these steps:
    1. In Class View, right-click MySampleClass, point to Add, and then click Add Method.
    2. In the Method Wizard, select the return type as int.
  6. Type myAbstractMethod as the Method name.
  7. Under the Method modifiers section, click to select Abstract.
  8. Click Finish.
  9. On the Build menu, click Build Solution.
You receive the compilation error that is described in the "Symptoms" section.

Modification Type:MinorLast Reviewed:1/18/2006
Keywords:kbBug kberrmsg kbwizard KB811599 kbAudDeveloper