By Ramkumar Durgam
// 'PhoneDirindex.cs - PhoneDirectory to Simulate The Use of an Multiparameter Indexer Using C #
/ / -------------------------------------------------------------------------------------------- -------------------------------------------------- ---
//
Using system;
Namespace PhoneDirIndexer
{
Class phoneDir
{
Private string [] _name;
Private uint [] _phonenumber;
Private ushort _size;
Public PhoneDir (Ushort size) // default constructor
{
_size = size;
_name = new string [size];
_phonenumber = new uint [size];
For (int i = 0; i { _Name [i] = "no name"; _phonenumber [i] = 0; } } // Indexer 1.0 - Read / Write Indexer (Returns the name and phoneenumber based on the position and sets the name) Public String this [int POS] { get { Console.write ("Calling Get INDEXER 1.0 ..."); Return (_Name [POS] "(" _PhoneNumber [POS]) ")") } set { Console.WriteLine ("Calling Set On Indexer 1.0 ..."); _Name [POS] = Value; } } // Indexer 2.0 - Read / Write Indexer (Returns the name and phoneenumber based on the position and sets the name) Public String this [string name] { get { Console.write ("Calling Get INDEXER 2.0 ..."); INT POS; POS = -1; For (int i = 0; i <_size; i ) { IF (_name [i] == name) { POS = i; Break; } } IF (POS> = 0) Return (_Name [POS] "(" _PhoneNumber [POS]) ")") Else ""; " } set { Console.writeline ("Calling Set On Indexer 2.0 ..."); INT POS; POS = -1; For (int i = 0; i <_size; i ) { IF (_name [i] == name) { POS = i; Break; } } IF (POS> = 0) _phonenumber [pos] = convert.touint16 (value); } // Indexer 3.0 - Read / WRITE INDEXER TO RETURN THE NAME for a Given Phone Number And Updates A Phonenumber for a Given Phone Number Public String this [uint phone] { Get // Returns the name for a Given Phone Number { Console.write ("Calling Get INDEXER 3.0 ..."); INT POS = -1; For (int i = 0; i <_size; i ) { IF (_phonenumber [i] == phone) { POS = i; Break; } } IF (POS> = 0) Return (_Name [POS]); Else ""; " } Set // set (Updates) The Phone Number (s) for a Given Phone Number { Console.writeline ("Calling Set On Indexer 3.0 ..."); For (int i = 0; i <_size; i ) { IF (_phonenumber [i] == phone) _phonenumber [i] = convert.touint16 (value); } } } // Indexer 4.0 - Readonly Indexer (Returns The Position for a Given Name and Phonenumber Combination) Public int this [String Name, uint phone] { get { Console.write ("Calling Readonly Indexer 4.0 ..."); INT POS = -1; For (int i = 0; i <_size; i ) { IF (_name [i] == name && _phonenumber [i] == phone) { POS = i; Break; } } Return POS; } } } Class PhoneDirui { Static void main (string [] args) { INT nooflistings = 5; PhoneDir Listing = New PhoneDir (5); Listing [1] = "john.a"; // output: "Calling set on indexer 1.0 ..." Listing [3] = "john.b"; // output: "Calling set on indexer 1.0 ..." Listing [John.a "] =" 1111 "; // Output:" Calling set on indexer 2.0 ... " Listing [1111u] = "1234"; // Output: "Calling set on indexer 3.0 ..." // NOTE: 'u' (Next to the number) is The the Only One That Causes Indexer3.0 To Be Called // WITHOUT That Indexer 1.0 Would Have Been Called Becuase It Thinks Its An Integer And Not A Uint Listing ["john.b"] = "2222"; // output: "Calling set on indexer 2.0 ..." For (int i = 0; i { Console.writeline ("Listing [{0}]: {1}", I, listing [i]); // output: "Calling get on indexer 1.0 ... Listing [i]: _name (_phone) } Console.writeLine ("Person Located in Phone Number 1111 ..." Listing [1111u]); // Output: Calling Get INDEXER 3.0 ... Person Located in Phone Number 1111 ... Console.writeline ("Person Located in Phone Number 1234 ..." Listing [1234U]); // Output: Calling Get on Indexer 3.0 ... Person Located in Phone Number 1234 .... John.a (Because We Updated The Phone Number Above) Console.writeline ("John.b, 2222 IS located in ..." Listing ["john.b", 2222]); // Output: Calling Readonly Indexer 4.0 ... john.b, 2222 is located in ... 3 Console.readline (); } } } --------------------- OUTPUT -------------------- Calling set on indexer 1.0 ... Calling set on indexer 1.0 ... Calling set on Indexer 2.0 ... Calling set on Indexer 3.0 ... Calling set on Indexer 2.0 ... Calling get on indexer 1.0 ... listing [0]: no name (0) Calling get on indexer 1.0 ... Listing [1]: john.a (1234) Calling get on indexer 1.0 ... listing [2]: no name (0) Calling get on indexer 1.0 ... Listing [3]: john.b (2222) Calling get on indexer 1.0 ... Listing [4]: no name (0) Calling get on Indexer 3.0 ... Person Located in Phone Number 1111 ... Calling get on indexer 3.0 ... Person Located in Phone Number 1234 ... john.a