Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't need an initializer (static constructor). if agencyId is an integer, try This</p> <pre><code>public static class AgentDescriptions { private static readonly Dictionary&lt;int, int&gt; dic = new Dictionary&lt;int, int&gt;(); public static int GetId(int agencyId) { if (!dic.ContainsKey(agencyId)) Adic.dd(agencyId, GetIDFromDB(agencyID)); return dic[agencyId]; } // ... </code></pre> <p>and use it like this:</p> <pre><code> if (something == AgentDescriptions.GetId(agencyId)) //Blah(); </code></pre> <p>or, if agencyId is a string, or you want to use strings like "P1", "P2" etc as keys, then</p> <pre><code>public static class AgentDescriptions { private static readonly Dictionary&lt;string, int&gt; dic = new Dictionary&lt;string, int&gt;(); public static int GetId(string agencyId) { if (!dic.ContainsKey(agencyId)) Adic.dd(agencyId, GetIDFromDB(agencyID)); return dic[agencyId]; } // ... </code></pre> <p>and use it like this:</p> <pre><code> if (something == AgentDescriptions.GetId("P1") //Blah(); </code></pre> <p>if the list of Agencies is fixed, you can add preconfigured static members to retrieve the Id for those agencies...</p> <pre><code>public static class AgentDescriptions { private static readonly Dictionary&lt;string, int&gt; dic = new Dictionary&lt;string, int&gt;(); public static int P1 { get { return GetId("P1"); } } public static int P2 { get { return GetId("P2"); } } public static int P3 { get { return GetId("P3"); } } public static int GetId(string agencyId) { if (!dic.ContainsKey(agencyId)) dic.Add(agencyId, 12); return dic[agencyId]; } </code></pre> <p>and use it like this:</p> <pre><code> if (something == AgentDescriptions.P1) //Blah(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload