Adarsh Babu PR
Adarsh Babu PR

Reputation: 197

How to reinitiate all properties in a static class in c#

public static class MySampleClass
{
     public static string sampleProperty1
     {
            get { return GetValue("sampleProperty1"); }
     }
     public static string GetValue(string Key)
    {
       // Here is the code to get value from table based Key.
          return Key;
    }
}

I have many static properties in MySampleClass like SampleProperty1... once I have set values for the properties, I'm not able to reset the values to same properties.

Upvotes: 1

Views: 706

Answers (1)

pkuderov
pkuderov

Reputation: 3551

You probably don't. Static properties is almost every time is a code smell, especially mutable. Make them non-static and use instances of your class.

In this case you can implement this class as every new instance initialized with new portion of values from some table. And you won't have a problem of reinitialization - just create new instance of this type and it'll be initialized with fresh new values.


Your current implementation doesn't require reinitialization - the properties of MySampleClass always get actual value from table. If that instance of table is outdated, get fresh one and replace old one with this new one - after that properties of MySampleClass will return fresh values.

Upvotes: 1

Related Questions