// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; namespace E { public class Person { private string _name; private string _address; public Person(string name, string address) { _name = name; _address = address; } public string Name { get { return _name; } set { _name = value; } } public string Address { get { return _address; } set { _address = value; } } public override string ToString() { return "{" + Name + "," + Address + ")"; } } public class Job { private string _title; private decimal _salary; public Job(string title, decimal salary) { _title = title; _salary = salary; } public string Title { get { return _title; } set { _title = value; } } public decimal Salary { get { return _salary; } set { _salary = value; } } public override string ToString() { return "{" + Title + "," + Salary + ")"; } } public class PersonList { private List data = new List(); public void Add(Person p) { data.Add(p); } public Person this[int ix] { get { return data[ix]; } } } public class JobList { private List data = new List(); public void Add(Job j) { data.Add(j); } public Job this[int ix] { get { return data[ix]; } } } public class Index { private int _ix; public Index(int ix) { _ix = ix; } public int Ix { get { return _ix; } set { _ix = value; } } } public class PersonIndex : Index { public PersonIndex(int ix) : base(ix) { } } public class JobIndex : Index { public JobIndex(int ix) : base(ix) { } } public class EmployeeList { private PersonList persons = new PersonList(); private JobList jobs = new JobList(); public void Add(Person p, Job j) { persons.Add(p); jobs.Add(j); } public Person GetPerson(int ix) { return persons[ix]; } public Job GetJob(int ix) { return jobs[ix]; } public Person this[PersonIndex ix] { get { return GetPerson(ix.Ix); } } public Job this[JobIndex ix] { get { return GetJob(ix.Ix); } } } public class Program { public static void Main(string[] args) { EmployeeList lst = new EmployeeList(); lst.Add(new Person("A A", "A road 123"), new Job("Developer", 10000.00m)); lst.Add(new Person("B B", "B ave 456"), new Job("Tester", 9000.00m)); Console.WriteLine(lst.GetPerson(0)); Console.WriteLine(lst.GetJob(1)); Console.WriteLine(lst[new PersonIndex(0)]); Console.WriteLine(lst[new JobIndex(1)]); } } }