BUG: Microsoft IntelliSense cannot list properties that only have one accessor in an interface (814742)



The information in this article applies to:

  • Microsoft Visual C# .NET (2003)

SYMPTOMS

In Microsoft Visual C# .NET 2003, when you type a period (".") after the name of a variable, Microsoft IntelliSense may not list properties of the corresponding class. Instead, IntelliSense may list the get_PropertyName and the set_PropertyName methods, where PropertyName is a property of your class.

Additionally, if your class is a control, and if you add this control to a form or to a user control, you may notice one or more design-time errors in the Task List window that are similar to the following:
Operation is not valid due to the current state of the object.

At least one element in the source array could not be cast down to the destination array type.

Type 'MyClass' does not have a constructor with parameters of types String.

Object type cannot be converted to target type.
Note This bug does not appear in Microsoft Visual Studio .NET 2002.

CAUSE

If a property in an interface has only one accessor that is declared, and if your class implements the interface, you may implement the get accessor and the set accessor. Then you build your class, you add a project reference from another project to the built assembly, and then you declare a variable that has your class as the data type. When you type the name of the declared variable and then type a period, IntelliSense incorrectly imports your property. Therefore, IntelliSense does not list your property and only lists methods that correspond to the get_PropertyName method and the set_PropertyName method that is mentioned in the "Symptoms" section of this article. Additionally, you may notice errors similar to the errors in the "Symptoms" section of this article because of a special case where objects are replaced by their names.

Note
  • This bug and the related errors are noticed only at design time. Even though this bug exists, you may build and may run your projects as expected.
  • You notice this bug only if you add a project reference to your class from another project. If you use your class in the same project as the class file, you do not notice the behavior that is mentioned in the "Symptoms" section of this article.

WORKAROUND

To work around this bug, you may modify your class, you may modify your interface, or you may ignore the bug. Microsoft recommends that you modify your class so that your class implements only the accessor that is declared in your interface.

Modify Your Class

Modify your class so that your class implements only the accessor that is declared in your interface. To do this, follow these steps for the sample that is created in the "Steps to Reproduce This Behavior" section of this article:
  1. Press ESC to ignore the IntelliSense list that is displayed when you type MyReference..
  2. Press BACKSPACE to delete the period (".") that you typed in step 13 of the "Steps to Reproduce This Behavior" section of this article.
  3. Switch to ClassLibrary1, and then open Class1.cs.
  4. Locate and then delete the following code:
    set
    {
       MyInt = value;
    }
  5. On the Build menu, click Build ClassLibrary1.
  6. Switch to ConsoleApplication1.
  7. In Solution Explorer, expand References.
  8. Right-click ClassLibrary1, and then click Remove.
  9. On the Project menu, click Add Reference.
  10. In the Add Reference dialog box, click Browse.
  11. Locate and select the ClassLibrary1.dll file that you built in step 5, and then click Open.
  12. In the Add Reference dialog box, click OK. This adds a project reference to ClassLibrary1.dll to ConsoleApplication1.
  13. In Class1.cs, type . where you pressed BACKSPACE in step 2. The IntelliSense list is displayed together with MyProperty.
Note These steps are based on the sample from the "Steps to Reproduce the Behavior" section of this article. Therefore, the code and the file names in these steps may differ from your code and your file names.

Modify Your Interface

Modify your interface so that your interface declares both accessors for your property. To do this, follow these steps for the sample that is in the "Steps to Reproduce This Behavior" section of this article:
  1. Press ESC to ignore the IntelliSense list that is displayed when you type MyReference.
  2. Press BACKSPACE to delete the period (".") that you typed in step 13 of the "Steps the Reproduce This Behavior" section of this article.
  3. Switch to ClassLibrary1, and then open Class1.cs.
  4. Locate the following code:
    get;
  5. Add the following code to Class1.cs, after the code that you located in step 4:
    set;
  6. On the Build menu, click Build ClassLibrary1.
  7. Switch to ConsoleApplication1.
  8. In Solution Explorer, expand References.
  9. Right-click ClassLibrary1, and then click Remove.
  10. On the Project menu, click Add Reference.
  11. In the Add Reference dialog box, click Browse.
  12. Locate and select the ClassLibrary1.dll file that you built in step 6, and then click Open.
  13. In the Add Reference dialog box, click OK. This adds a project reference to ClassLibrary1.dll to ConsoleApplication1.
  14. In Class1.cs, type . where you pressed BACKSPACE in step 2. The IntelliSense list is displayed together with MyProperty.
Note These steps are based on the sample from the "Steps to Reproduce This Behavior" section of this article. Therefore, the code and the file names in these steps may differ from your code and your file names.

Ignore the Bug

You notice this bug only at design time. Therefore, you may ignore the bug and then build and run your application as expected. To do this, follow these steps for the sample in the "Steps to Reproduce This Behavior" section of this article:
  1. Press ESC to ignore the IntelliSense list that is displayed when you type MyReference..
  2. Type MyProperty.
You may then use MyReference.MyProperty in your project.

Note These steps are based on the sample from the "Steps to Reproduce This Behavior" section of this article. Therefore, the code and the file names in these steps may differ from your code and your file names.

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.
  2. Use Visual C# .NET to create a new Class Library Project. By default, Class1 is created.
  3. On the View menu, click Code.
  4. Replace the existing code with the following code:
    using System;
    
    namespace ClassLibrary1
    {
       public interface MyInterface
       {
          int MyProperty
          {
             get;
          }
       }
    
       public class MyClass: MyInterface
       {
          private int MyInt = 0;
    
          public int MyProperty
          {
             set
             {
                MyInt = value;
             }
    
             get
             {
                return MyInt;
             }
          }
       }
    }
  5. On the Build menu, click Build ClassLibrary1.
  6. On the File menu, point to Add Project, and then click New Project.
  7. In the Add New Project dialog box, click Visual C# Project under Project Types. Under Templates, click Console Application, and then click OK. By default, Class1 is created.
  8. On the Project menu, click Add Reference.
  9. In the Add Reference dialog box , click Browse.
  10. Locate and select the ClassLibrary1.dll file that you have built in step 5, and then click Open.
  11. In the Add Reference dialog box, click OK. This adds a project reference to ClassLibrary1.dll to ConsoleApplication1.
  12. Add the following code to the Main method:
    ClassLibrary1.MyClass MyReference = new ClassLibrary1.MyClass();
  13. On the next line, type MyReference. to see the behavior that is mentioned in the "Symptoms" section of this article.

Modification Type:MinorLast Reviewed:1/13/2006
Keywords:kbpending kbConsole kbIDEProject kbDevStudio kbProperties kbide kbProgramming kbbug KB814742 kbAudDeveloper