PRB: Cannot Access Static Members of Visual J# .NET Interface in Visual C# .NET or Visual Basic .NET (815253)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual J# .NET (2002)
  • Microsoft Visual J# .NET (2003)

SYMPTOMS

When you implement an interface that is declared in Visual J# .NET and that has a static member variable in your Visual C# .NET class or your Visual Basic .NET class, you may receive the following error message during compilation:

Visual C# .NET Error
'MyTestJSharpClassLibrary.IMyJSharpInterface' does not contain a definition for 'MyStaticMember'
Visual Basic .NET Error
'MyTestConsoleApplication.MyTestClass' must implement 'Public Const MyStaticMember As Integer = 0' for interface 'MyTestJSharpClassLibrary.IMyJSharpInterface'.

CAUSE

In Microsoft Visual J# .NET, you cannot declare static methods in an interface, but you can declare static member variables in an interface. In Visual C# .NET and in Visual Basic .NET, you cannot declare static members in an interface. Therefore, you cannot access the static member of your Visual J# .NET interface.

WORKAROUND

Microsoft recommends that you not add static members in an interface. Instead of using interfaces, use abstract classes, and declare static members. For example, you can use the following abstract class code for the Visual J# .NET interface in the "More Information" section:
package MyTestJSharpClassLibrary;

public abstract class MyTestAbstractClass
{
	public static int MyStaticMember; // Static member variable
	public int NonStaticMember()
	{
		return 0;
	}
}

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual J# Projects under Project Types, and then click Class Library under Templates.
  4. In the Name box, type MyJSharpClassLibrary, and then click OK.
  5. Replace the existing code with the following code:
    package MyTestJSharpClassLibrary;
    
    public interface IMyJSharpInterface
    {
    	static int MyStaticMember=0; // Static member variable
    	int NonStaticMember();  // Non-static member
    }
  6. On the File menu, point to New, and then click Project.
  7. Click Visual C# Projects or Visual Basic Projects under Project Types, and then click Console Application under Templates.
  8. In the Name box, type MyTestConsoleApplication, and then click OK.
  9. Replace the existing code with the following code:
    Visual C# .NET Code
    using System;
    using MyTestJSharpClassLibrary;
    
    namespace MyCSharpApplication
    {
    	class MyCSharpClass : IMyJSharpInterface
    	{
          // Implement non-static member of the interface
          public int NonStaticMember()
          {
             return 10;
          }
    
    		[STAThread]
    		static void Main(string[] args)
    		{
             IMyJSharpInterface myObj = new MyCSharpClass();
             // Access non-static member
             Console.WriteLine(myObj.NonStaticMember());
             // Access static member
             Console.WriteLine(myObj.MyStaticMember);
             
    		}
    
    	}
    }
    
    Visual Basic .NET Code
    Imports MyTestJSharpClassLibrary
    
    Module Module1
    
       Sub Main()
          Dim myObj As IMyJSharpInterface
          myObj = New MyTestClass()
          ' Access non-static member
          Console.WriteLine(myObj.NonStaticMember())
          ' Access static member
          Console.WriteLine(myObj.MyStaticMember)
       End Sub
    
    End Module
    Public Class MyTestClass
       Implements IMyJSharpInterface
       ' Implement non-static member
       Public Function NonStaticMember() As Int32
          Return 10
       End Function
    End Class
    
  10. In Solution Explorer, right-click MyTestConsoleApplication, and then click Add Reference.
  11. On the Projects tab, click MyJSharpClassLibrary, click Select, and then click OK.
  12. On the Debug menu, click Start. You receive the compilation error that is listed in the "Symptoms" section of this article.

REFERENCES

For more information about Visual J#, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MajorLast Reviewed:8/7/2003
Keywords:kberrmsg kbCompiler kbJava kbprb KB815253 kbAudDeveloper