user2032433
user2032433

Reputation:

Where to set static member's properties?

If I have a static member variable in my class, where should I initialize it, and set all of its properties?

class Foo
{
    static public Timer t;
    private int foo;

    public Foo(int f)
    {
        this.foo = f;
    }
}

As you can see, my class has member variable private int foo which is set to match the constructor's parameter. I also got static public Timer t which is supposed to tick for each isntance of this class. Now my question is, where should I add this code:

t = new Timer();
t.Interval = 1;

Since if I add it to my class' constructor, it will be called every time when a new instance is created, which is not what I want. I could move the t = new Timer(); to the actual variable declaration like so: static public Timer t = new Timer(); but I would still have to insert t.Interval = 1; somewhere.

So the question is, how do I initialize a static member - and how do I edit its properties - only once, and not every time I create a new instance?

Upvotes: 2

Views: 98

Answers (5)

Matthew Watson
Matthew Watson

Reputation: 109567

I'm going to give you slightly different advice from most of the other replies.

I'm saying that you should avoid a static constructor if possible.

The reason is one of efficiency. The details are too complex to go into here, but see these pages for details:

http://ericlippert.com/2013/02/06/static-constructors-part-one/

http://blogs.msdn.com/b/brada/archive/2004/04/17/115300.aspx?Redirected=true

To be honest, it's probably not too much to worry about, but because it's so simple to avoid a static constructor, you should probably do so.

What you do is to write a static method which will return a value with which you can initialise your static field.

For your Timer example it would look like this:

private static Timer _timer = InitTimer();

private static Timer InitTimer()
{
    Timer result = new Timer {Interval = 100};
    return result;
}

Although for a simple initialisation like that, it's not even necessary to write a separate method, since you can just do this:

private static Timer _timer = new Timer {Interval = 100};

But in more complex situations, you can write a static method.

Upvotes: 3

Sam
Sam

Reputation: 42377

Here are two ways to do this:

Initialiser

static public Timer t = new Timer
{
    Interval = 1
};

Static constructor

static public Timer t;

static Foo
{
    t = new Timer();
    t.Interval = 1;
}

Upvotes: 0

bash.d
bash.d

Reputation: 13207

There are static constructors. Invoke them like this

class Foo {

static Foo(){
// initialize your timer here
}

See here.

Upvotes: 1

Jesse van Assen
Jesse van Assen

Reputation: 2290

Static Constructors are used for this.

Upvotes: 2

RMalke
RMalke

Reputation: 4094

You can add this code in a Static Constructor, like this:

static Foo()
{
    t = new Timer();
    t.Interval = 1;
}

From MSDN:

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

Upvotes: 2

Related Questions