-2

What I have is:

public static class IDs {

    public static string someID { get; set; }

    static IDs() {
        log.info(someID);
        // use someID here
    }
}

public class otherClass {

     public void otherMethod(string sym) {
         IDs.someID = sym;
     }
}

and then using an instance of otherClass like this:

otherClassInstance.otherMethod("someStringSymbol");

I dont have any build errors, but log.info(someID); is printing null.
I was expecting it to be someStringSymbol.

8
  • 2
    The order in which static constructors happen is not defined. (The order in which static initializers happen is, but even then not across classes.) The ill-defined order, as well as the near impossibility to recover from errors in them, should be a strong disincentive against using static constructors period, but especially when you're going to introduce dependencies. Prefer instances with a clear initialization time, even if you make those singletons. Commented Jul 17, 2019 at 14:34
  • stackoverflow.com/questions/185384/… Commented Jul 17, 2019 at 14:34
  • The code you posted does not compile. Please edit your question with a minimal, reproducible example. Commented Jul 17, 2019 at 14:36
  • 2
    The static constructor is called before the someID property is set. This is the expected behaviour. The property cannot be set before the type has been initialized. Commented Jul 17, 2019 at 14:37
  • what are you trying to achieve? Commented Jul 17, 2019 at 14:41

2 Answers 2

3

This is because the static constructor is called automatically before the first instance is created or any static members are referenced..

This means that when an instance of otherClass invokes IDs.someID = sym; the first operation that gets executed is the static constructor, i.e. the code inside static IDs().

At this point the static variable has not yet been initialized, and you are basically executing log.info(null);.

After the static constructor completes, the variable is initialized, so you should be able to see its value inside otherMethod, after the first reference of IDs.


Given the OP's requirement:

I want to use the value passed in someID in a switch statement

The solution could be to simply execute a static method whenever a new value is set, with the help of explicit getters and setters:

public static class IDs
{
    private static string _someID; // backing field

    public static string SomeID
    {
        get { return _someID; }

        set
        {
            _someID = value;
            DoSomethingWithSomeID();
        }
    }

    private static DoSomethingWithSomeID()
    {
        // Use SomeID here.

        switch (IDs.SomeID)
        {
            ...
        }
    }
}

public class OtherClass
{
    public void OtherMethod(string sym)
    {
        // This will set a new value to the property
        // and invoke DoSomethingWithSomeID.
        IDs.SomeID = sym;
    }
}

DoSomethingWithSomeID will be invoked every time someone sets a new value to SomeID.

Sign up to request clarification or add additional context in comments.

10 Comments

So how can we initialize someID at same time as executing constructor static IDs
do you just want to log the value of someID?
No, I want to use it in a switch statement and based on the value passed in someID from someStringSymbol , construct xPath of several UI elements in IDs class
are the references to those UI elements static fields?
Yes , thats the challenge I am facing currently. Initially I used static but now as requirements changed , I need to construct xPath dynamically
|
1

I dont think what you are trying to do is suited to static classes. I would try the following

public class IDs{

    public string someID{ get; set; }

    public IDs(string someId){
        this.someID = someId;
        log.info(this.someID);
           //use someID here
    }
}

pulic class otherClass{


     public otherMethod(string sym){
                IDs id = new IDs(sym);
            }
      }

public class anotherClass{

                //access instance of otherClass in wrp and call otherMethod()
               wrp.otherMethod("someStringSymbol")
         }

1 Comment

If I do this , then I will have to change reference of IDs at several places because I am making it non-static from static. I want to avoid that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.