Search code examples
c#windowsoopextend

C# Windows Phone Using same value in different pages


namespace Challenge
{
    class Constss
    {
        public int score= 0;

    }
}

I have a class which has public int type. And I am using it in MainPage.xml.cs class.

Constss c = new Constss();
c.score = 100; 

When I want to use it in Step2 class, score value is again 0. How can I keep the value of score variable ?


Solution

  • I'm assuming you are instantiating two separate instances of Constss. This way, they are separate objects and will have their own (separate) values.

    You can solve this a couple of ways:

    • Instantiate just one instance of Constss, and reference the single instance from whichever class needs it.
    • Declare Constss as a static class (each member must also be static). In this case you don't instantiate the class, you just refer to it. Any reference to the class is referring to the same instance with just one set of values.

    Learn more about static classes on MSDN.