Inherit from messagebox in c#

A

Adam

I am trying to inherit from MessageBox and i get error in
compilation time.

The code is:
public class MyMessageBox : MessageBox
{
public static DialogResult Show(int errorCode)
{
return DialogResult.OK;
}
}

The error is:
System.Windows.Forms.MessageBox.MessageBox() is
inaccessible due to its protection level

I guess it because the constructor of MessageBox is privte.
My question is, is there anyway to inherit from messagebox?

Thanks,
Adam
 
J

Jon Skeet

Adam said:
I am trying to inherit from MessageBox and i get error in
compilation time.

The code is:
public class MyMessageBox : MessageBox
{
public static DialogResult Show(int errorCode)
{
return DialogResult.OK;
}
}

The error is:
System.Windows.Forms.MessageBox.MessageBox() is
inaccessible due to its protection level

I guess it because the constructor of MessageBox is privte.
My question is, is there anyway to inherit from messagebox?

No. Unless a class has at least one protected or public (or internal if
you're in the same assembly) constructor, you can't inherit from it (in
C# at least).
 
W

Willy Denoyette [MVP]

Adam wrote:
|| I am trying to inherit from MessageBox and i get error in
|| compilation time.
||
|| The code is:
|| public class MyMessageBox : MessageBox
|| {
|| public static DialogResult Show(int errorCode)
|| {
|| return DialogResult.OK;
|| }
|| }
||
|| The error is:
|| System.Windows.Forms.MessageBox.MessageBox() is
|| inaccessible due to its protection level
||
|| I guess it because the constructor of MessageBox is privte.
|| My question is, is there anyway to inherit from messagebox?
||
|| Thanks,
|| Adam

No, the class has only a private constructor, so you can't derive from nor create an instance of the class.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top