Monday, August 30, 2010

Changing constant value at runtime

In C#, you are allowed to set the value for a constant. This can be done by using the "readonly" keyword.

Normally, when we want to declare a constant that is not updateable through out the application by doing the following:

   public const int MAX_DAYS = 30;


In case, your application would like to allow the system administrator changing the MAX_DAYS value to accommodate their business environment, you need a way to do it. This can be done by replacing the "const" keyword with "readonly":

public class MyConstants
(
public readonly int MAX_DAYS;

public MyConstants()
{
MAX_DAYS = {read the value from database OR config file};
}

)

No comments:

Post a Comment