BUG: "Cannot refer to an instance member of a class" error message occurs when you have an assembly that uses an Enum and Class method of the same name (814602)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

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 "Applies to" section.

MORE INFORMATION

Steps to Reproduce the Problem

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

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

    By default, Module1.vb is created.
  5. 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
  6. On the Debug menu, click Start to run the project.

    You receive the error message described in the "Symptoms" section of this article.
  7. In the MyTestMethod code, replace
    color.red
    -with-
    TestBinding.color.red
  8. On the Debug menu, click Start to run the project again.

    The problem is resolved.

REFERENCES

For more information about shared keywords in Visual Basic .NET, click the following article number to view the article in the Microsoft Knowledge Base:

308371 How to create and use shared members by using Visual Basic .NET


Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbvs2002sp1sweep kberrmsg kbCompiler kbbug KB814602 kbAudDeveloper