.net - Help with static constructor in c# -
i need initializing static readonly variables in c#. have class signature
public class agentdescriptions { public static readonly int p1; public static readonly int p2; static agentdescriptions() { int agencyid = 1; //i need pass in constructor somehow p1 = getidfromdb(agencyid); p2 = getidfromdb(agencyid); } }
p1 , p2 used on , on in application , trying avoid 2 things. 1)magic numbers , 2)trip db every time need use p1 , p2.
in application using them in many places in manner
if (something == agentdescriptions.p1) //blah();
please help. how can pass agencyid in static constructor? if add contructor , pass agencyid there, have initialize class every time use it? mean trip db every time?
why class static. if you're passing variable in constructor you're implying instance of object state, not class.
i'd make variables private member variables methods available them. have constructor takes agency id , sets 2 variables. if need maintain single instance of type, use singleton (static function in class stores single instance of object or creates new object if 1 doesn't exist yet). on other hand, if need multiple objects different agency ids, you've got mechanism that.
Comments
Post a Comment