sgtz
sgtz

Reputation: 9009

using static fields in a class

So I have a field in a class, but for some reason it is comming back with an uninitialised value of 0001.01.01.

 private static DateTime start = new DateTime(2011, 1, 1);

There's another static method used as an initializer in another field.

 private static readonly DateTime[] dates = SetupDates(20 * 12);


 private static DateTime[] SetupDates(int n){
    var d = start;
    ....
 }

I thought that "new" in start would need to be completed before before SetupDates could continue... so local variable d would contain 2011.1.1. Looks like I'm mistaken, and I should use a static constructor instead. Is this behaviour to be expected?

Upvotes: 4

Views: 593

Answers (5)

CodingBarfield
CodingBarfield

Reputation: 3398

Simply move all Init code to the static constructor and execute it in the Order you want it to execute. Case closed ;)

Upvotes: 2

Giorgio Minardi
Giorgio Minardi

Reputation: 2775

Do not understand you question, can you please post only the piece of code causing problems ? The code above doesn't compile. You can initialize a static field with a New object.

     var test = new Test();
     test.SetupDates();

If you put a breakpoint in the SetupDates method the date will be 1/1/2011

  public class Test {
        private static DateTime start = new DateTime(2011, 1, 1);       


        public void SetupDates()
        {
            //breakpoint here
            var d = start;

        }
    }

Upvotes: 0

Ray
Ray

Reputation: 46585

The order matters here.

The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration.

Make sure your start static field is declared first. Or better yet use a static constructor so you're not relying on the order of fields.

For example this works:

    private static DateTime start = new DateTime(2011, 1, 1);
    private static readonly DateTime[] dates = SetupDates(20 * 12);

But this doesn't

    //Bad SetupDates relies on start which is not initialized
    private static readonly DateTime[] dates = SetupDates(20 * 12); 
    private static DateTime start = new DateTime(2011, 1, 1);

Assuming you change SetupDates to return a DateTime[]

Upvotes: 6

Tigran
Tigran

Reputation: 62248

No the static fields initialized before you call SetupDates(), there is something other mistaken in your code, which is not visible from the code actually provided.

For example I see the declaration:

private void SetupDates(int n)

but also

private static readonly DateTime[] dates = SetupDates(20 * 12);

EDIT

If SetupDates() intializaes the static field , like in code provided (but I repeat the code is not correct as is), you should keep attention on order of intialization. In that case there could be possible case when the SetupDates() is called before start intialized.

The function doesn't return anything, it couldn't possibly even compile.

Upvotes: 1

flyx
flyx

Reputation: 39668

You cannot call the instance method SetupDates for constructing the static field dates. This code should not compile.

Upvotes: 1

Related Questions