Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could use a static dictionary as a map instead of a switch-statement.</p> <pre><code> static readonly Dictionary&lt;string, string&gt; map = new Dictionary&lt;string, string&gt; { { "00", "14" }, { "01", "18" }, // ... more ... }; // ... in your method ... string str = pk.Substring(2, 2); string val; if (!map.TryGetValue(str, out val)) { // Handle error, like in the "default:" case of the switch statement } else { ViewBag.Type = _reference.Get(val, model.Type).Value; } </code></pre> <p>However, I would only do this, if there are really a lot of mappings that maybe can even be "read" from an external source like a configuration file.</p> <p>Also note, that if the "key" is really a consecutive sequence of integers starting at 0, you might be able to use an array, where the "key" is simply the index into it.</p> <pre><code> static readonly string[] map = new string[] { "14", "18", ... }; int index = Int32.Parse(pk.Substring(2, 2)); // Error handling elided. if (index &lt; 0 || index &gt; map.Length) { // Handle error, like in the "default:" case of the switch statement } else { ViewBag.Type = _reference.Get(map[index], model.Type).Value; } </code></pre> <p>Otherwise rather stay with an explicit switch statement (possibly factoring out the assignment for more terse code):</p> <pre><code> string val; switch (pk.Substring(2, 2)) { case "00": val = "14"; break; case "01": val = "18"; break; // ... more ... default: // Error handling for unknown switch-value. break; } ViewBag.Type = _reference.Get(val, model.Type).Value; </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