473,783 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!

Sep 29 '08 #1
1 2023
On Mon, 29 Sep 2008 10:33:51 -0700, Scott Stark <em***@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
Sep 29 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4720
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e = IntVar() for part, list in info.masterList.items():
0
1402
by: mahesh | last post by:
Hi all, I have a doubt with OOAD and Dialogs. I have a Dialog Class. tkSimple.py It contains a class body as below. Now I am calling the tkSimple.py in another python program and using the default body, and everything from tkSimple.py. Now my requirement is as follows I need to popup a couple of other dialog boxes with a little difference in between each dialog and take
17
2676
by: Mark | last post by:
uhhmmm... not really sure how to word this. i cant get get this to compile.. i'm not sure what the proper syntax to do this is.. hopefully it's self explanatory. here's my class: ------ class TileMap { public:
2
2351
by: Steve | last post by:
I'm in a weird situation where I'm using ComboBox's in a DataGrid. When the ComboBox selection changes, I'm currently storing the SelectedValue object into the DataSource of the DataGrid cell. This is not what I want. I want to bind a string to the datagrid, then bind the string and value of a lookup table to the combo box. When the combobox lookup is changed, I wanted to store the value in a different, unbound property of the bound...
6
1442
by: thomasp | last post by:
For those who gave advice on the shortfalls of my first attempt at writing a vb.net class, Thank You. I hope that I was able to apply some of your advice to this larger atempt. At first I didn' t really see an advantage of a Class over a module containing the same functions, but now that this Class is working for me, I have found a possible use. Since this class will hold all the values used in a report I am building, I think I will now...
0
1426
by: Svenn Bjerkem | last post by:
Hi, Armed with Programming Python 3rd Edition and Learning Python 2nd edition I try to write an application which I at first thought was simple, at least until I was finished with the GUI and then wanted to start putting some data into it. As you will see, the program is not yet finished but I post what I have so far.
1
1072
by: Will | last post by:
We use Public properties in our business classes to represent field values from databases. So in Vb, I might have something like: Public Property Emp_Id() As System.Decimal Get Return _Emp_Id End Get Set(ByVal value As System.Decimal) _Emp_Id = value End Set
0
1513
by: Hans Koller | last post by:
Hello group, I design a class to bind it to a property grid for easy modification of some settings. My problem is now that I want to raise an event when a settings has been changed. Thats not a problem with some "normal" properties (see property DisplayGrid). But when I use a subclass and TypeConverter the setter of the property is not called. Set(ByVal value As LineSetting) m_Limit1 = value
1
1929
by: steve | last post by:
Hi, I am working on a finite-volume numerical method project. I am not great at programming though. I am looking for the best way to represent this grid in C. The grid consists of N points spaced at deltaX = Xmax/N where Xi = (i-1/2)*deltaX, i = 1, 2, 3,...N. Furthermore there are cell interfaces located at Xi+1/2 and Xi-1/2 and each interface has a + and - side. Obviously I can declare Q and get Q, Q, Q for the grid points but I am not...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.