Note that there are some explanatory texts on larger screens.

plurals
  1. POBest hybrid approach to a multi-dimensional array with strong typed indexing
    primarykey
    data
    text
    <p>I have what amounts to a multi-dimensional array.</p> <pre><code>int[][][] MyValues; </code></pre> <p>What I want is to access the indexes via a strongly typed equivelent, such as an enumeration. I'm aware that you can get the enumeration values from the Enum type, but it's a bit long winded for my tastes.</p> <p>I'd rather have a way to Strongly type the indexes.</p> <p>For example:</p> <pre><code>int CarNumber = MyValues[Racetrack.Daytona][Race.Daytona500][Driver.JGordon]; </code></pre> <p>This would, by virtue of it being enum-like, prevent any out of bounds exceptions from being thrown, plus it gives all the indexes a nice human readable meaning.</p> <p>I've implemented this using a dictionary approach, but it seems kind of heavy handed:</p> <pre><code>Dictionary&lt;Racetrack,Dictionary&lt;Race,&lt;Dictionary&lt;Driver,int&gt;&gt;&gt; = new Dictionary&lt;Racetrack,Dictionary&lt;Race,&lt;Dictionary&lt;Driver,int&gt;&gt;&gt;(); </code></pre> <p>which I can then access via enums, but I don't really like this approach. It seems "ugly".</p> <p><strong>I'm looking for some alternate methods to represent what is essentially a multi-dimensional array while using human readable indexers, while maintaining type safety (can't accidently use a Driver for a Race, for instance, so simply using consts is not a good approach).</strong></p> <p>Any suggestions?</p> <p>This will be a compile time array (example above is not real, just an illustration) so I don't have to worry about inserts or deletes or other manipulations of the array. It will stay immutable, both in values, size and layout.</p> <p>Using a static class with const values is not a good approach either, since it doesn't enforce that only the set of values defined can be passed as indexers.</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.
 

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