Static abstract class

I need a way to create a static class where some constants can be specific, but hardcoded.

What I really want to do is a class in which several extended constants are provided when the class is extended - I want the "constants" to be hard-coded. I decided that I would make some abstract properties and define get {return constant; } when extending a class.

I know this is not possible, so now I have two options, and I wonder what would be better and why (if there are options that I miss, let me know!)

  • Create a static class with fields with a null value and throw an exception if the fields have a null value when the static method is called.
  • Discard the static class. Have a non-static class with abstract properties and instantiate the object wherever I need it, although all the functionality is really static.

I know that this can be subjective and case-dependent, however I go around in circles thinking about it and really can do it with some external input. This is a plus, I hope that there can be something to do what I want, and I just think about it wrong.

Update:. Code: I will try to write code that describes what I would like to accomplish. I know this code cannot work!

Imagine that the abstract Calculation class is in a dll that is used by many projects. The functionality is the same for all of them, just the constant varies from project to project.

public abstract static class Calculation { private abstract int Constant { get; } //The constant is unknown at this time public static int Calculate(int inputValue) { return inputValue * Constant; } } 

The Calc class is defined in a separate project where functionality is required and a constant is known.

 public static class Calc : Calculation { private override int Constant { get { return 2; } } ... static class Program { [STAThread] static void Main() { //At some point: int result = Calc.Calculate(6); } } 

I suppose the easiest way would be to create a non-static class and instantiate, however, I fear that multiple instances of the class might be expensive and would like to prevent this if possible.

I don’t see how I could write this as a singleton template without writing it again in every project - having only the Nested class in the dll. This will not prevent the developer from simply creating a regular class and will probably restart the discussion for each project that uses code.

Update # 2 . What can I say with option 1:

Class in dll:

 public static class Calculation { public int? Constant {get; set;} public static int Calculate(int inputValue) { if (Constant == null) throw new ArgumentNullException(); return inputValue * (int)Constant; } } 

Using the function in a separate project:

 static class Program { [STAThread] static void Main() { //At some point: Calculation.Constant = 2; int result = Calc.Calculate(6); } } 

Option one is very simple and elegant, which worries me that nothing forces the creator to set a constant. I fear (admittedly, unlikely) a scenario where an obscure edge case will cause the property to not be set and the code to fail (and the constant to be the last suspect) ...

+7
source share
4 answers

You can make non-static classes that follow a singleton, ensuring only one instance of the object exists. I think this may be the next best.

+6
source

You cannot statically inherit at the same time! It just makes sense!

If you need to override behavior, you need inheritance!

If you need ease of calling (one of the benefits of statics), you can use Factory (or singleton if only one instance is required)

I assume that you will probably have to rethink your model. This set of your constants is probably what you could extract in a separate class and then pass that class to your static method. Will this fit your needs?

+5
source

Edit

To your sample code:

 public abstract static class Calculation { public static int Constant { get; set; } public static int Calculate(int i) { return i * Constant; } } // ... Calculation.Constant = 6; Calculation.Calculate(123); 

Somewhat more general:

 public abstract static class Calculation { public struct Context { public int Constant, SignificantDigits; public bool Radians; } public static int Calculate(int i, Context ctx) { return i * ctx.Constant; } } // ... Calculation.Calculate(123, new Calculate.Context { Constant = 6 }); 

First idea:

The closest that I can think of are generics:

 public interface ISpecifics { void DoSomething(); string SomeProp { get; } } public static class Static<S> where S : ISpecifics, new() { public static string ExerciseSpecific() { var spec = new S(); spec.DoSomething(); return spec.SomeProp; } } 

Or if you really need one static type

 public static class Static { public static string ExerciseSpecific<S>() where S : ISpecifics, new() { var spec = new S(); spec.DoSomething(); return spec.SomeProp; } } 

Does it help?

+1
source

I needed almost the same thing, so first I created a non-static class with all the functions. Then a static class that creates one such non-static class in its static constructor. Then, any of the static methods invokes the corresponding instance methods.

Something like that:

 public class CalculationInstance { private int constant; public int Calculate(int inputValue) { return inputValue * constant; } public void AnyOtherMethod() { .... } public CalculationInstance(int constant) { this.constant=constant; } } public static class Calculation { const int CONSTANT=2; private CalculationInstance calc; static Calculation() { calc=new CalculationInstance(CONSTANT); } public static int Calculate(int inputValue) { return calc.Calculate(inputValue); } public static void AnyOtherMethod() { calc.AnyOtherMethod(); } } static class Program { [STAThread] static void Main() { //At some point: int result = Calculation.Calculate(6); } } 
0
source