create barcode image vb.net Figure 6-18. The syntax and meaning of the set accessor declaration in C#.NET

Printing Data Matrix in C#.NET Figure 6-18. The syntax and meaning of the set accessor declaration

Figure 6-18. The syntax and meaning of the set accessor declaration
Generating ECC200 In C#
Using Barcode drawer for VS .NET Control to generate, create Data Matrix image in VS .NET applications.
www.OnBarcode.com
Recognizing Data Matrix 2d Barcode In Visual C#
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
CHAPTER 6 MORE ABOUT CLASSES
Paint Barcode In C#.NET
Using Barcode creator for Visual Studio .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
Barcode Creator In Visual C#.NET
Using Barcode creator for .NET framework Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
The get Accessor
Draw QR Code In Visual C#
Using Barcode generation for VS .NET Control to generate, create QR-Code image in .NET framework applications.
www.OnBarcode.com
UPC-A Supplement 5 Generation In Visual C#.NET
Using Barcode creator for VS .NET Control to generate, create UPC Code image in .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. The syntax and meaning of the get accessor are shown in Figure 6-19. 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.
Paint GS1 128 In Visual C#.NET
Using Barcode generation for Visual Studio .NET Control to generate, create UCC-128 image in .NET applications.
www.OnBarcode.com
Generating USS-93 In C#.NET
Using Barcode encoder for .NET framework Control to generate, create ANSI/AIM Code 93 image in Visual Studio .NET applications.
www.OnBarcode.com
Figure 6-19. The syntax and meaning of the get accessor declaration
Scanning ECC200 In Visual C#.NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
DataMatrix Drawer In VS .NET
Using Barcode drawer for Reporting Service Control to generate, create Data Matrix 2d barcode image in Reporting Service applications.
www.OnBarcode.com
CHAPTER 6 MORE ABOUT CLASSES
QR Code Creator In None
Using Barcode maker for Online Control to generate, create QR image in Online applications.
www.OnBarcode.com
Draw USS Code 128 In Java
Using Barcode drawer for BIRT Control to generate, create USS Code 128 image in BIRT reports applications.
www.OnBarcode.com
More About Indexers
Encoding UCC - 12 In VB.NET
Using Barcode creation for Visual Studio .NET Control to generate, create UPC A image in .NET applications.
www.OnBarcode.com
Read Data Matrix ECC200 In .NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in Visual Studio .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 emp1[0] = "Doe"; string NewName = emp[0]; Index
Encoding Code 39 Full ASCII In None
Using Barcode encoder for Microsoft Word Control to generate, create Code-39 image in Microsoft Word applications.
www.OnBarcode.com
Linear Maker In Java
Using Barcode drawer for Java Control to generate, create Linear image in Java applications.
www.OnBarcode.com
// Calls set accessor // Calls get accessor
PDF 417 Recognizer In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Generating UPC Symbol In .NET
Using Barcode creation for ASP.NET Control to generate, create GS1 - 12 image in ASP.NET applications.
www.OnBarcode.com
Declaring the Indexer for the Employee Example
QR Drawer In Objective-C
Using Barcode creator for iPhone Control to generate, create QR Code 2d barcode image in iPhone applications.
www.OnBarcode.com
Printing UCC.EAN - 128 In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create GS1 128 image in ASP.NET applications.
www.OnBarcode.com
The following code declares an indexer for the earlier example: class Employee. 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 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 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.
CHAPTER 6 MORE ABOUT CLASSES
class Employee { public string LastName; public string FirstName; public string CityOfBirth;
// Call this field 0. // Call this field 1. // Call this field 2.
public string this[int index] // Indexer declaration { set // Set accessor declaration { switch (index) { case 0: LastName = value; break; case 1: FirstName = value; break; case 2: CityOfBirth = value; break; default: // (Exceptions in Ch. 11) throw new ArgumentOutOfRangeException("index"); break; } } get { // Get accessor declaration switch (index) { case 0: return LastName; case 1: return FirstName; case 2: return CityOfBirth; default: // (Exceptions in Ch. 11) throw new ArgumentOutOfRangeException("index"); break; } } } }
CHAPTER 6 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".
// Private field // Private field // The indexer
// Return value of either Temp0 or Temp1
CHAPTER 6 MORE ABOUT CLASSES
Indexer Overloading
A class can have more than one indexer, as long as the parameter lists are different; it isn t sufficient for the indexer type to be different. This is called indexer overloading, because all the indexers have the same name the this access reference. For example, the following class has three indexers: two of type string, and one of type int. Of the two indexers of type string, one has a single int parameter and the other has two int parameters. class MyClass { public string this [ int index ] { get { ... } set { ... } } public string this [ int index1, int index2 ] { get { ... } set { ... } } public int this [ float index1 ] { get { ... } set { ... } } ... }
Copyright © OnBarcode.com . All rights reserved.