tags:

views:

200

answers:

4

I have a Winform in c# and I'm trying to build a hashtable with a list of buttons that I have. IE: Monday0700Button, Monday0730Button, Monday0800Button, and so on. In the hashtable I am trying to order these, so that if I need to use a button, and the button that follows it, I can use the hashtable key. IE: if I need to color the background of Monday0730 and Monday 0800, I can somehow tell it to find the next key in the list.

How do I add these buttons into a hashtable so that I can use them in this capacity. Thanks.

A: 

Hashtables have no inherent order. There is no "next" key in a standard hashtable.

You actually used the name of the data structure that you probably should use: List. Lists will enable you to go "forward" and "backward" as needed.

Michael Todd
A: 

This is difficult to answer without more information, but, based on what you have here, the first thing that comes to mind is this. If you set the IDs of your buttons to Monday0700Button or Monday0730Button, add just those strings to a list.

List<string> myButtons = new List<string>();

myButtons.Add( "Monday0700Button" );
myButtons.Add( "Monday0730Button" );
myButtons.Add( "Monday0800Button" );
myButtons.Add( "Monday0830Button" );

Then when you need to use one, use 'FindControl' to find the instance of the button using the name.

int index = myButtons.IndexOf( clickedButtonId );
FindControl( myButtons[index++] );

You would not want to put instances of the buttons into a Hashtable/dictionary, I would not think.

Sean
A: 

A doubly-linked list would be more apropriate data structure for both forward and backward iteration where each list item serves as a data source for your buttons (e.g. left-wise button would traverse to the left node, and the right-wise button would traverse to the right node). It doesn't really make sense to store buttons in the data structure at this point in the discussion, but instead store information which describes each node so that static buttons can have their text updated appropriately.

You can also achieve this using a List, however, it would require more code within the presentation tier.

Shaun Wilson
+2  A: 
Abel