Microsoft KB Archive/814602

= BUG: &quot;Cannot refer to an instance member of a class&quot; error message occurs when you have an assembly that uses an Enum and Class method of the same name =

Article ID: 814602

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



SYMPTOMS
When you have an assembly that uses an Enum method and a Class method and both methods have the same name, you may receive the following error message:

Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.



CAUSE
You receive this error message when you refer to Enum in shared methods of the Class. The compiler incorrectly binds to the instance method of the Class. The compiler is supposed to bind to the Enum.



WORKAROUND
To work around this problem, refer to Enum with a fully-qualified name, as in the following example:

Namespace.Enum.MemberName



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to Reproduce the Problem
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New and then click Project. In the Project Types section, click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types. In the Templates section, click Console Application, name the project TestBinding, and then click OK.

By default, Module1.vb is created.  Replace the existing code in Module1.vb with the following code: Namespace TestBinding

' Declare an Enum. Enum color As Integer red = 200 End Enum

Class TestClass 'Declare a method that has the same name ' as that of enum. Public Function color As color Return color.red + 50 End Function

' Try to access the Enum from this shared method. Public Shared Sub MyTestMethod 'Refer to Enum. Console.WriteLine(color.red) End Sub End Class

Module Module1 Sub main ' Call the shared method. TestClass.MyTestMethod End Sub End Module

End Namespace  On the Debug menu, click Start to run the project.

You receive the error message described in the &quot;Symptoms&quot; section of this article.</li>  In the MyTestMethod code, replace color.red -with- TestBinding.color.red </li> On the Debug menu, click Start to run the project again.

The problem is resolved.</li></ol>

<div class="references_section">