Use an index to fetch items from a record
using System;
Class indexerrecord{
Private string[] Data= new string [6];
Private string[] keys = {
"Author", "Publisher", "Title" ,
"Subject", "ISBN", "Comments"
};
The program uses two methods index, one is an integer to subscript, the first keyword (string) as subscript
public string this [int idx]{
Set
{
if (idx>=0 && idx<data.length)
Data[idx]=value;
}
Get
{
if (idx>=0 && idx <data.length)
return DATA[IDX];
return NULL;
}
}
public string this[string key]
{
Set
{
IND idx = FindKey (key);
THIS[IDX] = value;
}
Get
{
return This[findkey (key)];
}
}
private int FindKey (string key)
{
for (int i=0;i<keys.length;i++)
if (keys[i] = = key) return i;
return-1;
}
static void Main ()
{
Indexrecord record = new Indexrecord ();
Record[0] = "Mark Twain";
RECORD[1] = "Crox Publishing Company";
Record[2] = "Adventures of Tom Sawyer";
Console.WriteLine (record["Title"]);
Console.WriteLine (record[ "Author" ]);
Console.WriteLine (record[ "Publisher" ]);
}
}
Property
Class Person
{
public string Name{set;get;}
}
C # Properties and indexes