Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are correct in that you cannot have a <code>static virtual</code>/<code>override</code> property. This can be annoying for the case you describe, which I too have struggled with in the past.</p> <p>However, you can use <code>static</code> properties... and the <code>new</code> keyword to hide the base implementation.</p> <p>For example, let's define some units here:</p> <pre><code>public abstract class BaseUnit { public static int UnitCost { get { return 10; } } } public class CheapUnit : BaseUnit { new public static int UnitCost { get { return 5; } } } public class ExpensiveUnit : BaseUnit { new public static int UnitCost { get { return 20; } } } public class MultipleUnit : BaseUnit { new public static int UnitCost { get { return BaseUnit.UnitCost * 4; } } } </code></pre> <p>Then a little test program to output their values:</p> <pre><code> public void Run() { Console.WriteLine("Base unit cost:\t\t{0}", BaseUnit.UnitCost); Console.WriteLine("Cheap unit cost:\t{0}", CheapUnit.UnitCost); Console.WriteLine("Expensive unit cost:\t{0}", ExpensiveUnit.UnitCost); Console.WriteLine("Multiple unit cost:\t{0}", MultipleUnit.UnitCost); } </code></pre> <p>And we get...</p> <pre><code>Base unit cost: 10 Cheap unit cost: 5 Expensive unit cost: 20 Multiple unit cost: 40 </code></pre> <p>Ah ha! Exactly what we want. I'm not 100% happy with this solution but I don't know a better way without having to create an instance which I think it silly to access a polymorphic class constant. So I just do it this way.</p> <p><strong>EDIT:</strong> In cases where I found myself doing this, I often opted to move this kind of 'lookup' logic into a manager (singleton) class. For example, <code>UnitManager</code> or similar, where I could pass in a type name (<code>"CheapUnit"</code>) and it would look up the cost in a <code>Dictionary</code>. </p> <p>Just a quick mockup idea:</p> <pre><code>sealed class UnitManager { static readonly UnitManager instance = new UnitManager(); public static UnitManager Instance { get { return instance; } } Dictionary&lt;string, int&gt; unitCostDictionary = new Dictionary&lt;string, int&gt;(StringComparer.OrdinalIgnoreCase); // Ignore Case of Keys public int LookupUnitCost(string unitType) { int unitCost = 0; unitCostDictionary.TryGetValue(unitType, out unitCost); return unitCost; } } </code></pre> <p>This also allows dynamic unit cost and centralized notifications when a unit cost may change.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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