Static classes must derive from object (C#)
I am having a problem in C#, the output states:
Error 1 Static class 'WindowsFormsApplication1.Hello2'
cannot derive from type 'System.Windows.Forms.Form'. Static classes
must derive from object.
How could I correct this?
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
开发者_开发技巧 private void button1_Click(object sender, EventArgs e)
{
Hello2.calculate();
}
}
public class Hello : Form
{
public string test { get; set; }
}
public static class Hello2 : Form
{
public static void calculate()
{
Process.Start("test.exe");
}
}
It means that static
classes can't have : BaseClass
in the declaration. They can't inherit from anything. (The inheritance from System.Object
is implicit by declaring nothing.)
A static
class can only have static
members. Only instance members are inherited, so inheritance is useless for static
classes. All you have to do is remove : Form
.
Is there some reason to derive Hello and Hello2 from Form? If not, just do:
public static class Hello2
{
...
}
likewise for class Hello
精彩评论