Business web site - Chapter 8 Object-Based Programming 325 After setting value

Chapter 8 Object-Based Programming 325 After setting value by dimension name Before getting value by index number After getting value by index number Fig. 8.16 Fig. 8.1FiFig. 8.16g. 8.16Indexers provide subscripted access to an object s members. (Part 6 of 6.) Fig. 8.16 The privatedata members of class Boxare stringarray names(line 16), which contains the names (i.e., “length”, “width”and “height”) for the dimensions of a Box, and doublearray dimensions(line 17), which contains the size of each dimension. Each element in array names corresponds to an element in array dimensions (e.g., dimensions[2]contains the height of the Box). Boxdefines two indexers (lines 28 42 and lines 45 72) that each returna double value representing the size of the dimension specified by the indexer s parameter. Indexers can be overloaded like methods. The first indexer uses an intsubscript to manipulate an element in the dimensions array. The second indexer uses a string subscript representing the name of the dimension to manipulate an element in the dimensions array. Each indexer returns -1if its getaccessor encounters an invalid subscript. Each indexer s setaccessor assigns valueto the appropriate element of dimensionsonly if the index is valid. Normally, the programmer would have an indexer throw an exception if an indexer received an invalid index. We discuss how to throw exceptions in Chapter 11, Exception Handling. Notice that the string indexer uses a while structure to search for a matching string in the names array (lines 64 66). If a match is found, the indexer manipulates the corresponding element in array dimensions(line 69). Class IndexerTest is a System.Windows.Forms.Form that manipulates the private data members of class Box through Box s indexers. Instance variable box is declared at line 96 and initialized in the constructor at line 105 with dimensions of 0.0. The event handler for button Get Value by Index (lines 139 145) invokes method ShowValueAtIndex( lines 118 122) to retrieve the value at the index number specified in index- TextBox. The event handler for button Set Value by Index (lines 148 156) assigns the
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Leave a Reply