Finishing the Sample Application


To illustrate how HashTables can be used in a real-world application, let's add one to the sample application. The CodeGridWeb control will let developers assign a unique name to each cell. We will implement this feature by adding the unique name to a HashTable as the key, then record the row and column for the cell as the item in the HashTable.

To finish the sample application:

  1. First add the declaration for the HashTable in Figure 9.63 .

    Figure 9.63 The Hashtable field in our grid control will store the keys the developer assigns to each cell.
     Hashtable hsCells = new Hashtable(); 
  2. The Key is a property of the Cell class. However, all the keys need to be stored in the CodeGridWeb control. So we're going to add a function to the CodeGridWeb class so that the Cell class can set the contents of the HashTable. Add the code in Figure 9.64 to the CodeGridWeb control.

    Figure 9.64 SetCellKey store Keys for each cell in a HashTable. The HashTable stores keys and values. The Key is provided by the developer. The value is a string we build that records the row and the column of the cell formatted as "row;column;" ("5;10;" for example).
     private void SetCellKey(string key, int row, int col) {    hsCells[key] = row.ToString() + ";" +                  col.ToString() + ";"; } 
  3. With that function in place, we can add a property to the Cell class to set the Key. Add the code in Figure 9.65 to the Cell class.

    Figure 9.65 Now you see why the Cell class needs a pointer back to the CodeGridWeb object. It needs it so that it can call the SetCellKey private function. It can call a private function on the parent because it's a nested class. It's defined inside of the CodeGridWeb class, which means that it has access to all private fields and members of the outer class.
     public string Key {    get    {       return _key;    }    set    {       _key = value;       _parent.SetCellKey(_key,_row,_col);    } } 
  4. Now, in order for the developer to locate a Cell based on the key, we need to add another indexer to the CodeGridWeb class. This indexer will take as a parameter a string rather than a row and a column. The code for the new indexer is in Figure 9.66 .

    Figure 9.66 This indexer lets you specify the key for a cell. The function then looks up the key in the HashTable. The item for the key is a string in the form "row;column;". We then split the string into the row and column portions and use those values to locate the cell in the array.
     public Cell this[string key] {    get    {       Cell retVal = null;       string cellIndex =       (string)hsCells[key];       if (cellIndex != null &&       cellIndex != "")       {          string[] coords =          cellIndex.Split(';');          int r = System.Convert.          ToInt32(coords[0]);          int c = System.Convert.          ToInt32(coords[1]);          retVal = _cells[r,c];       }       return retVal;    } } 

graphics/tick.gif Tip

  • The CodeGridWeb control is now fully functional. To test the control, read the next section.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net