Microsoft KB Archive/815253

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

Article ID: 815253

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition

-



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 &quot;More Information&quot; 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.



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Click Visual J# Projects under Project Types, and then click Class Library under Templates. In the Name box, type MyJSharpClassLibrary, and then click OK.  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 }  On the File menu, point to New, and then click Project.</li> Click Visual C# Projects or Visual Basic Projects under Project Types, and then click Console Application under Templates.</li> In the Name box, type MyTestConsoleApplication, and then click OK.</li>  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 </li> In Solution Explorer, right-click MyTestConsoleApplication, and then click Add Reference.</li> On the Projects tab, click MyJSharpClassLibrary, click Select, and then click OK.</li> On the Debug menu, click Start. You receive the compilation error that is listed in the &quot;Symptoms&quot; section of this article.</li></ol>

<div class="references_section">