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"); } }
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"; // ... }
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With