You receive error messages when you try to inherit from a class that contains only private constructors in Visual Studio .NET or in Visual Studio 2005 (833898)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 2.0
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio 2005 Standard Edition

SYMPTOMS

If you try to inherit from a class that contains only private constructors, you receive many error messages. This problem may occur in many different scenarios.

CAUSE

You receive these error messages because you cannot inherit from a class than contains only private constructors.

RESOLUTION

To resolve this problem, do not try to inherit from a class that contains only private constructors. For more information, see the "References" section of this article.

MORE INFORMATION

Steps to reproduce the behavior

To notice the types of error messages that you may receive if you try to inherit from a class that contains only private constructors, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. Use Microsoft Visual C# .NET or Microsoft Visual C# 2005, or Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 to create a Microsoft Windows application project that is named AccessLevelTest. By default, the Form1 form is created.
  3. On the View menu, click Code.
  4. Follow these steps, depending on whether you are using Visual C# .NET or Visual C# 2005, or Visual Basic .NET or Visual Basic 2005:
    • If you are using Visual C# .NET or Visual C# 2005, follow these steps:
      1. In the Form1.cs file, locate the following code:
              Application.Run(new Form1());
           }
        }
      2. Paste the following code after the code that you located in the previous step:
        public class MyClass : System.Drawing.Brush
        {
           public override object Clone()
           {
              return null;
           }
        }
      3. On the Build menu, click Build AccessLevelTest. You receive the following error message in the Task List window:
        'System.Drawing.Brush.Brush()' is inaccessible due to its protection level
    • If you are using Visual Basic .NET or Visual Basic 2005, follow these steps:
      1. In the Form1.vb file, locate the following code:
        End Class
      2. Paste the following code after the code that you located in the previous step:
        Class MyBrush
           Inherits System.Drawing.Brush
        
           Public Overloads Overrides Function Clone() As Object
           End Function
        
        End Class
        You receive the following error message in the Task List window:
        Class 'MyBrush' must declare a 'Sub New' because its base class 'Brush' does not have an accessible 'Sub New' that can be called with no arguments.
      3. In the Form1.vb file, locate the following code:
        Class MyBrush
           Inherits System.Drawing.Brush
      4. To resolve the error that you received in step b, paste the following code after the code that you located in the previous step:
        Sub New()
        End Sub
        You receive the following error message in the Task List window:
        First statement of this 'Sub New' must be a call to 'MyBase.New' or 'MyClass.New' because base class 'System.Drawing.Brush' of 'AccessLevelTest.Form1.MyBrush' does not have an accessible 'Sub New' that can be called with no arguments.
      5. In the Form1.vb file, locate the following code:
        Sub New()
      6. To resolve the error message that you received in step d, paste the following code after the code that you located in the previous step:
        MyBase.New()
        • If you are using Visual Basic .NET 2003, you receive the following error message in the Task List window:
          'System.Drawing.Brush.Private Sub New()' is not accessible in this context because it is 'Private'.
        • If you are using Visual Basic .NET 2002, you receive the following error message in the Task List window:
          'System.Drawing.Brush.Private Overloads Sub New()' is not accessible in this context because it is 'Private'.
        Additionally, Dynamic Help for this error message suggests that, to correct this error, you must remove the Private access modifier from the method definition in the base class. However, you cannot modify the base class (System.Drawing.Brush) because it is part of the Microsoft .NET Framework.

Modification Type:MajorLast Reviewed:2/10/2006
Keywords:kbvs2005swept kbvs2005applies kbprb kbSample kberrmsg kbcode KB833898 kbAudDeveloper