Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static classes must derive from object (C#)

Tags:

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");          }     } 
like image 285
Matt Avatar asked Jun 27 '11 17:06

Matt


People also ask

How do you derive a static class?

The solution couldn't be simpler: public class A { public const string ConstantA = "constant_a"; public const string ConstantB = "constant_b"; // ... } public class B : A { public const string ConstantC = "constant_c"; public const string ConstantD= "constant_d"; // ... }

Can we use static class as base class in C#?

Not possible to inherit static class in c#. Basic usage of the static class is to make method and member static and can invoke by using name of the class without creating the object of the static class. The main purpose of the static class is to create only one istance of the member in the memory.

Can we create object of static class?

You cannot create an object of a static class and cannot access static members using an object. C# classes, variables, methods, properties, operators, events, and constructors can be defined as static using the static modifier keyword.

Can static class inherit abstract class?

It's not allowed because inheritance is about creating related classes of objects, and you're not creating any objects at all with static classes.


1 Answers

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.

like image 61
Joel B Fant Avatar answered Oct 26 '22 22:10

Joel B Fant