barcode generator project in vb.net Figure 6-17. Comparing an indexer declaration to a property declaration in C#.NET

Painting ECC200 in C#.NET Figure 6-17. Comparing an indexer declaration to a property declaration

Figure 6-17. Comparing an indexer declaration to a property declaration
Encode Data Matrix In C#
Using Barcode creation for .NET framework Control to generate, create Data Matrix 2d barcode image in .NET framework applications.
www.OnBarcode.com
Decoding ECC200 In Visual C#
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
MORE ABOUT CLASSES
PDF417 Generation In Visual C#
Using Barcode creator for Visual Studio .NET Control to generate, create PDF417 image in Visual Studio .NET applications.
www.OnBarcode.com
EAN-13 Maker In C#.NET
Using Barcode encoder for .NET framework Control to generate, create UPC - 13 image in VS .NET applications.
www.OnBarcode.com
The Indexer set Accessor
Encoding UPC A In Visual C#.NET
Using Barcode creator for VS .NET Control to generate, create Universal Product Code version A image in Visual Studio .NET applications.
www.OnBarcode.com
Barcode Generation In C#.NET
Using Barcode generation for VS .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
When the indexer is the target of an assignment, the set accessor is called and receives two items of data, as follows: An implicit parameter, named value, which holds the data to be stored One or more index parameters that represent where it should be stored emp[0] = "Doe"; Index Value Parameter Your code in the set accessor must examine the index parameters, determine where the data should be stored, and then store it. Figure 6-18 shows the syntax and meaning of the set accessor. The left side of the figure shows the actual syntax of the accessor declaration. The right side shows the semantics of the accessor if it were written using the syntax of a normal method. The figure on the right shows that the set accessor has the following semantics: It has a void return type. It uses the same parameter list as that in the indexer declaration. It has an implicit value parameter named value, of the same type as the indexer.
Printing Barcode In C#
Using Barcode creator for VS .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
UPC-E Encoder In Visual C#
Using Barcode creation for VS .NET Control to generate, create UPC - E0 image in VS .NET applications.
www.OnBarcode.com
Figure 6-18. The syntax and meaning of the set accessor declaration
Data Matrix Maker In Objective-C
Using Barcode generation for iPhone Control to generate, create Data Matrix 2d barcode image in iPhone applications.
www.OnBarcode.com
Data Matrix 2d Barcode Generation In VS .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create ECC200 image in VS .NET applications.
www.OnBarcode.com
MORE ABOUT CLASSES
Encoding GS1 - 12 In .NET
Using Barcode creator for Reporting Service Control to generate, create GTIN - 12 image in Reporting Service applications.
www.OnBarcode.com
Generating EAN-13 In None
Using Barcode maker for Excel Control to generate, create EAN 13 image in Microsoft Excel applications.
www.OnBarcode.com
The Indexer get Accessor
QR Code Maker In .NET
Using Barcode drawer for ASP.NET Control to generate, create Quick Response Code image in ASP.NET applications.
www.OnBarcode.com
Linear 1D Barcode Generation In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create Linear Barcode image in ASP.NET applications.
www.OnBarcode.com
When the indexer is used to retrieve a value, the get accessor is called with one or more index parameters. The index parameters represent which value to retrieve. string s = emp[0]; Index parameter The code in the get accessor body must examine the index parameters, determine which field they represent, and return the value of that field. Figure 6-19 shows the syntax and meaning of the get accessor. The left side of the figure shows the actual syntax of the accessor declaration. The right side shows the semantics of the accessor if it were written using the syntax of a normal method. The semantics of the get accessor are as follows: It has the same parameter list as in the indexer declaration. It returns a value of the same type as the indexer.
Code 128A Maker In None
Using Barcode generator for Office Word Control to generate, create Code-128 image in Office Word applications.
www.OnBarcode.com
Code 39 Extended Drawer In None
Using Barcode generation for Online Control to generate, create Code 3 of 9 image in Online applications.
www.OnBarcode.com
Figure 6-19. The syntax and meaning of the get accessor declaration
Encode Code-39 In Java
Using Barcode generator for Java Control to generate, create Code-39 image in Java applications.
www.OnBarcode.com
GS1 RSS Encoder In Visual Studio .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create GS1 DataBar Stacked image in Visual Studio .NET applications.
www.OnBarcode.com
More About Indexers
UPC-A Supplement 5 Creation In None
Using Barcode generation for Software Control to generate, create UPC Symbol image in Software applications.
www.OnBarcode.com
Printing Barcode In VS .NET
Using Barcode drawer for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
As with properties, the get and set accessors cannot be called explicitly. Instead, the get accessor is called automatically when the indexer is used in an expression for a value. The set accessor is called automatically when the indexer is assigned a value with the assignment statement. When an indexer is called, the parameters are supplied between the square brackets. Index Value emp[0] = "Doe"; string NewName = emp[0]; Index
// Calls set accessor // Calls get accessor
Declaring the Indexer for the Employee Example
The following code declares an indexer for the earlier example: class Employee.
MORE ABOUT CLASSES
The indexer must read and write values of type string so string must be declared as the indexer s type. It must be declared public so that it can be accessed from outside the class. The three fields in the example have been arbitrarily indexed as integers 0 through 2, so the formal parameter between the square brackets, named index in this case, must be of type int. In the body of the set accessor, the code determines which field the index refers to and assigns the value of implicit variable value to it. In the body of the get accessor, the code determines which field the index refers to and returns that field s value.
class Employee { public string LastName; public string FirstName; public string CityOfBirth; public string this[int index] { set { switch (index) { case 0: LastName = value; break; case 1: FirstName = value; break; case 2: CityOfBirth = value; break;
// Call this field 0. // Call this field 1. // Call this field 2. // Indexer declaration // Set accessor declaration
default: // (Exceptions in Ch. 11) throw new ArgumentOutOfRangeException("index"); } } get { switch (index) { case 0: return LastName; case 1: return FirstName; case 2: return CityOfBirth; default: // (Exceptions in Ch. 11) throw new ArgumentOutOfRangeException("index"); } } } } // Get accessor declaration
MORE ABOUT CLASSES
Another Indexer Example
The following is an additional example that indexes the two int fields of class Class1: class Class1 { int Temp0; int Temp1; public int this [ int index ] { get { return ( 0 == index ) Temp0 : Temp1; } set { if( 0 == index ) Temp0 = value; else Temp1 = value; } } } class Example { static void Main() { Class1 a = new Class1(); Console.WriteLine("Values -- T0: {0}, T1: {1}", a[0], a[1]); a[0] = 15; a[1] = 20; Console.WriteLine("Values -- T0: {0}, T1: {1}", a[0], a[1]); } } This code produces the following output: Values -- T0: 0, T1: 0 Values -- T0: 15, T1: 20 // Note the implicit variable "value". // Note the implicit variable "value".
Copyright © OnBarcode.com . All rights reserved.