Alex Rivera | Logout

static abstract class

Asked 2011-09-13T08:00:00.350
13

I need a way to create a static class where some constants can be case specific, but hard-coded.

What I really want to do is have a class where several constants are provided when the class is extended - I want the 'constants' hard-coded. I figured I will make the some abstract properties and define the get { return constant; } when extending the class.

I know that is not possible, so now I am facing two options and am wondering what would be best and why (if there are options I'm missing please let me know!)

  1. Create a static class with nullable fields and throw an exception if the fields are null when the static method is called.
  2. Give up the static class. Have a non-static class with abstract properties and create an instance of the object wherever I need it even though all the functionality really is static.

I know this might be subjective and case-dependant, however I am going around in circles when thinking about this and could really do with some external input. That plus I hope there might be away of doing what I want and I'm just thinking about this wrong.

Update: Code: I will try to write some code that describes what I'd like to accomplish. I know this code can't work!

Imagine that the abstract class Calculation is in a dll, 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 class Calc is defined in a separate project where the functionality is needed and the Constant is known.

public static class Calc : Calculation
{
    private override int Constant { get { return 2; }
}

...

static class Program
{
    [
Edit
Report

1 Answer

7

You could make non-static classes that follow singleton, ensuring only one instance of the object ever to exist. I guess that could be the next best thing.

answered 2011-09-13T08:04:43.150

Your Answer