Andreas
Andreas

Reputation:

C# Static Variable forgets value

It sounds a little weird, but I've got some code (which actually is a plugin for MS Blend) that runs perfect in unit testing, but is not working within Blend.

The code is the following.

    private static volatile QWGUIRepository s_instance;

    public static void Initialize(IUnityContainer container, string themeuri)
    {
        lock (s_lock)
        {
            s_instance = new QWGUIRepository();
            QWRepository.Initialize(container);
        }
    }

In the debugger (after attaching it to Blend), I can see that s_instance gets initialized with a value (is not null afterwards), however as soon as I get out of the method "s_instance" will forget it's value and will be null.

Any ideas?

Thanks, Andreas

Upvotes: 1

Views: 915

Answers (3)

Andreas
Andreas

Reputation:

thanks for suggestions. Unfortunately (as usual) it was personal stupidity... The answer is - make sure you only use strong-named assemblies.

Andreas

Upvotes: 0

chris166
chris166

Reputation: 4807

I don't know anything about Blend, so this is a wild guess. Does Blend start the plugins in separate AppDomains? You can check that in the debug location toolbar in Visual Studio. That could explain it...

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1504122

Two guesses:

1) You're reading in a different AppDomain than you're writing in. Static variables are scoped by AppDomain. If you look at AppDomain.CurrentDomain in the debugger during Initialize and then when you're trying to read it, does it look like they're the same domain?

2) You've actually declared s_instance to be a local variable in Initialize, and aren't touching the static variable. Hopefully that's not the case, but you never know...

Upvotes: 7

Related Questions