Adding Indexers to the Sample Application


It would be very handy to add to a couple of indexers to the CodeGridWeb class. With an indexer a programmer would be able to write code like grid1[3,4].Text = "Something"; Without an indexer we would have to write a function to access the cells and the programmer would have to write something like grid1.SetCellText(3,4,Text) which isn't as pretty. So, let's add an indexer to the CodeGridWeb class.

To add an indexer to the sample code:

  • Add the code in Figure 9.47 to the CodeGridWeb class.

    Figure 9.47 The first indexer in the code lets you specify a row and a column and returns a cell from the _cells array. It's read-only, which means that a user can't create her own Cell objects and put them into the array; she can only modify existing cells.
     public Cell this[int row, int col] {    get    {       return _cells[row,col];    } } 

graphics/tick.gif Tip

  • The indexer in the sample code is readonly, but that doesn't prevent someone from setting the contents of the cell, only from replacing the cell object with their own object.




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