1

I have a static helper method (on a non-static class) that does some calculations, these calculations require a certain data object, in order to keep the the static method short and fast, I want to pre-process this data object and have the static method use it.

But where do I put this data object to be available to the static method, while making sure it is created only once? Should I just put it elsewhere in a singleton?

EDIT: I've been advised to use a static variable inside my class, I tried doing something like the following and when the static method tried to use it it was null:

private const int X = 50;
private const int Y = 10;

private static readonly List<double> CrossSetting = 
    (from horizontal in Enumerable.Range(0, X)
     from vertical in Enumerable.Range(0, Y)
     select Process(horizontal, vertical)).ToList();
Madd0g
  • 3,841
  • 5
  • 37
  • 59
  • 2
    It's not quite clear what you are trying to achieve. This is one of those things where you are probably asking the wrong question. Instead of trying to solve your actual problem, you're trying to solve a problem that you think will solve your actual problem. – Erik Funkenbusch Mar 23 '12 at 16:15
  • I am sure you can write the class as you say, I would like to see it analogously : a singleton+static objects+a static method. Please post a demo of what you say. – Sesama Sesame Mar 23 '12 at 16:22

3 Answers3

1

Singleton introduces global state, and is often criticized as an anti-pattern because of that. A good old private static variable inside your class would probably do just fine, unless you must make that preprocessed data available to other parts of your system.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Agreed, looks like premature optimisation to me. – KingCronus Mar 23 '12 at 16:16
  • I always need the data after I process it - I tried declaring a private static readonly variable and when I ran the code the variable was empty. See the update – Madd0g Mar 23 '12 at 16:36
  • @Madd0g This means that you are trying to use the variable earlier than it has been initialized. Read [this answer](http://stackoverflow.com/a/9399027/335858) to see how it is possible. – Sergey Kalinichenko Mar 23 '12 at 16:52
1

This might be a good time to introduce Lazy<T>:

class Helper
{
    private const int X = 50;
    private const int Y = 10;

    private static Lazy<List<double>> lazy = new Lazy<List<double>>(() => (from horizontal in Enumerable.Range(0, X)
                                                                           from vertical in Enumerable.Range(0, Y)
                                                                           select Process(horizontal, vertical)).ToList());
    // Didn't know what Process was
    static double Process(double h, double v)
    {
        return h * v;
    }

    public static void Method()
    {
        List<double> data = lazy.Value;
        foreach (double value in data)
            Console.WriteLine(value);
    }
}

The Helper.Method will create the data once and only once. It also has the benefit that the data will never be created if Helper.Method is never called. If you use static readonly, the data will be created the first time any member of the class is accessed. It is also thread safe.

Tergiver
  • 14,171
  • 3
  • 41
  • 68
0

Singleton is one option, but why can you not just make the data object static too?

Obviously you will need to make sure you are threadsafe if your static method may be accessed from multiple threads.

KingCronus
  • 4,509
  • 1
  • 24
  • 49