How to write a class to represent a grid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott Stark

    #1

    How to write a class to represent a grid

    What's the best way to represent data that exists in a grid structure? Let's
    say product #12345 is availabile in 3 colors, my grid might look like this:

    COLOR S M L XL
    Red x x x x
    Blue x x x
    Green x x

    I have a Product class and I basically want to create a property that will
    expose this set of information so that I can iterate over the available
    colors and determine the sizes availabe for that color.

    Thanks!

  • Peter Duniho

    #2
    Re: How to write a class to represent a grid

    On Mon, 29 Sep 2008 10:33:51 -0700, Scott Stark <email@scottsta rk.com>
    wrote:
    What's the best way to represent data that exists in a grid structure?
    Practically every question that asks "what's the best way..." is simply
    impossible to answer. The question never includes enough information for
    someone to describe the true one best way, assuming one exists at all.

    That said...
    Let's say product #12345 is availabile in 3 colors, my grid might look
    like this:
    >
    COLOR S M L XL
    Red x x x x
    Blue x x x
    Green x x
    >
    I have a Product class and I basically want to create a property that
    will expose this set of information so that I can iterate over the
    available colors and determine the sizes availabe for that color.
    It seems to me that one approach would be to use enumerations, with the
    colors being a regular linear enumeration, and the sizes being a [Flags]
    enumeration:

    enum Color { Red, Blue, Green };

    [Flags]
    enum Size { S = 1, M = 2, L = 4, XL = 8 };

    Then you can just store the state in an array:

    Size[] rgsizeColors =
    {
    Size.S | Size.M | Size.L | Size.XL,
    Size.M | Size.L | Size.XL,
    Size.S | Size.XL
    };

    Then just test for the individual sizes as you need to.

    An alternative would be to use regular enumerations for both values, and
    store a list of sizes for each color:

    enum Color { Red, Blue, Green };
    enum Size { S, M, L, XL };

    Size[][] rgrgsizeColors =
    {
    { Size.S, Size.M, Size.L, Size.XL },
    { Size.M, Size.L, Size.XL },
    { Size.S, Size.XL }
    };

    (or something like that...I wrote the initialization off the top of my
    head, don't recall for sure if C# supports that exact syntax and if so, if
    it requires a certain version. I'm sure you get the idea though).

    Testing for a given size is slightly harder because you have to enumerate
    the list for a given color, but enumerating all sizes for a color is
    slightly easier.

    Note that you could use List<Sizeinstan ces for the inner lists instead
    if you like, allowing the state to be more easily changed dynamically.

    Finally, taking your example to the literal extreme, you could use regular
    enumerations for both values and store a two-dimensional array of booleans:

    enum Color { Red, Blue, Green };
    enum Size { S, M, L, XL };

    bool[,] rgrgfSizesForCo lors =
    {
    { true, true, true, true },
    { false, true, true, true },
    { true, false, false, true }
    };

    (same caveat on the initializer)

    This is probably the least useful approach because you have to enumerate
    every size for every color to retrieve all of the information present in
    the data structure. But it's actually the closest to the way you stated
    the problem.

    These three examples are hardly the extent of all possible approaches.
    There are all sorts of variations on the theme that could be applied to
    the problem. Knowing what the _best_ way to do it depends on knowing much
    more about your overall program than we have. You can't expect complete
    strangers with no such knowledge to be able to tell you what's best. But
    hopefully with the above information, you can take another look at the
    problem and decide for yourself what suits the program in the best way. :)

    Pete

    Comment

    Working...