Microsoft KB Archive/814742

= BUG: Microsoft IntelliSense cannot list properties that only have one accessor in an interface =

Article ID: 814742

Article Last Modified on 5/31/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition

-





SYMPTOMS
In Microsoft Visual C# .NET 2003, when you type a period (&quot;.&quot;) after the name of a variable, Microsoft IntelliSense may not list properties of the corresponding class. Instead, IntelliSense may list the get_  and the set_  methods, where  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_  method and the set_  method that is mentioned in the &quot;Symptoms&quot; section of this article. Additionally, you may notice errors similar to the errors in the &quot;Symptoms&quot; 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 &quot;Symptoms&quot; 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 &quot;Steps to Reproduce This Behavior&quot; section of this article:  Press ESC to ignore the IntelliSense list that is displayed when you type MyReference. . Press BACKSPACE to delete the period (&quot;.&quot;) that you typed in step 13 of the &quot;Steps to Reproduce This Behavior&quot; section of this article. Switch to ClassLibrary1, and then open Class1.cs.  Locate and then delete the following code: set {  MyInt = value; }  On the Build menu, click Build ClassLibrary1. Switch to ConsoleApplication1.</li> In Solution Explorer, expand References.</li> Right-click ClassLibrary1, and then click Remove.</li> On the Project menu, click Add Reference.</li> In the Add Reference dialog box, click Browse.</li> Locate and select the ClassLibrary1.dll file that you built in step 5, and then click Open.</li> In the Add Reference dialog box, click OK. This adds a project reference to ClassLibrary1.dll to ConsoleApplication1.</li> In Class1.cs, type. where you pressed BACKSPACE in step 2. The IntelliSense list is displayed together with MyProperty.</li></ol>

Note These steps are based on the sample from the &quot;Steps to Reproduce the Behavior&quot; 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 &quot;Steps to Reproduce This Behavior&quot; section of this article: <ol> Press ESC to ignore the IntelliSense list that is displayed when you type MyReference. </li> Press BACKSPACE to delete the period (&quot;.&quot;) that you typed in step 13 of the &quot;Steps the Reproduce This Behavior&quot; section of this article.</li> Switch to ClassLibrary1, and then open Class1.cs.</li>  Locate the following code: get; </li>  Add the following code to Class1.cs, after the code that you located in step 4: set; </li> On the Build menu, click Build ClassLibrary1.</li> Switch to ConsoleApplication1.</li> In Solution Explorer, expand References.</li> Right-click ClassLibrary1, and then click Remove.</li> <li>On the Project menu, click Add Reference.</li> <li>In the Add Reference dialog box, click Browse.</li> <li>Locate and select the ClassLibrary1.dll file that you built in step 6, and then click Open.</li> <li>In the Add Reference dialog box, click OK. This adds a project reference to ClassLibrary1.dll to ConsoleApplication1.</li> <li>In Class1.cs, type. where you pressed BACKSPACE in step 2. The IntelliSense list is displayed together with MyProperty.</li></ol>

Note These steps are based on the sample from the &quot;Steps to Reproduce This Behavior&quot; 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 &quot;Steps to Reproduce This Behavior&quot; 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 &quot;Steps to Reproduce This Behavior&quot; section of this article. Therefore, the code and the file names in these steps may differ from your code and your file names.

<div class="status_section">

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

<div class="moreinformation_section">

Steps to Reproduce the Behavior
<ol> <li>Start Visual Studio .NET.</li> <li>Use Visual C# .NET to create a new Class Library Project. By default, Class1 is created.</li> <li>On the View menu, click Code.</li> <li> 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; }     }   } } </li> <li>On the Build menu, click Build ClassLibrary1.</li> <li>On the File menu, point to Add Project, and then click New Project.</li> <li>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.</li> <li>On the Project menu, click Add Reference.</li> <li>In the Add Reference dialog box, click Browse.</li> <li>Locate and select the ClassLibrary1.dll file that you have built in step 5, and then click Open.</li> <li>In the Add Reference dialog box, click OK. This adds a project reference to ClassLibrary1.dll to ConsoleApplication1.</li> <li> Add the following code to the Main method: ClassLibrary1.MyClass MyReference = new ClassLibrary1.MyClass; </li> <li>On the next line, type MyReference. to see the behavior that is mentioned in the &quot;Symptoms&quot; section of this article.</li></ol>

<div class="references_section">