I need help initializing static readonly variables in c#. I have a class with this signature
public class AgentDescriptions
{
public static readonly int P1;
public static readonly int P2;
static AgentDescriptions()
{
int agencyID = 1; //I need to pass this in the constructor somehow
P1 = GetIDFromDB(agencyID);
P2 = GetIdFromDB(agencyID);
}
}
P1 and P2 are used over and over in the application and I am trying to avoid two things. 1)Magic numbers and 2)trip to the DB every time I need to use P1 and P2.
In the application I am using them in many places in this manner
if (something == AgentDescriptions.P1)
//Blah();
Please help. How can I pass the agencyID in the static constructor? If I add another contructor and pass agencyID there, will I have to initialize the class every time I use it? will this mean a trip to the DB every time?