Microsoft KB Archive/833898

= 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 =

Article ID: 833898

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * 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 .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 &quot;References&quot; section of this article.



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:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. 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. On the View menu, click Code. 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:   In the Form1.cs file, locate the following code: Application.Run(new Form1); } } </li>  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; } } </li> 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

</li></ol> </li> If you are using Visual Basic .NET or Visual Basic 2005, follow these steps: <ol style="list-style-type: lower-alpha;">  In the Form1.vb file, locate the following code: End Class </li>  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.

</li>  In the Form1.vb file, locate the following code: Class MyBrush Inherits System.Drawing.Brush </li>  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.

</li>  In the Form1.vb file, locate the following code: Sub New </li>  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 <ul> 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'.

</li> 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'.

</li></ul>

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. </li></ol> </li></ul> </li></ol>

<div class="references_section">