1using System;
2
3namespace DataBindToCustomObject
4{
5	//UserDetails Class - Custom object
6	public class UserDetails
7	{
8		//Constructor
9		public UserDetails()
10		{
11		}
12
13		//Private variables
14		private string mEmailAddress = string.Empty;
15		private string mFirstName = string.Empty;
16		private string mLastName = string.Empty;
17
18		//Public properties
19		public string EmailAddress
20		{
21			get { return mEmailAddress; }
22			set { mEmailAddress= value; }
23		}
24		public string FirstName
25		{
26			get { return mFirstName; }
27			set { mFirstName = value.Trim(); }
28		}
29		public string LastName
30		{
31			get { return mLastName; }
32			set { mLastName = value.Trim(); }
33		}
34	}
35
36	//Implementation of IEnumerable: Gets an Enumerator.
37	public class Users : System.Collections.IEnumerable
38	{
39		//ArrayList -- handles all the details for the collection
40		private System.Collections.ArrayList marrUsers = new System.Collections.ArrayList();
41	
42		//Enumerator of the collection
43		public System.Collections.IEnumerator GetEnumerator()
44		{
45			return this.marrUsers.GetEnumerator();
46		}
47
48		//Add new user
49		public void Add(UserDetails objUser)
50		{
51			this.marrUsers.Add(objUser);
52		}
53
54		//Remove a user
55		public void Remove(UserDetails objUser)
56		{
57			this.marrUsers.Remove(objUser);
58		}
59
60		//Gets or sets a user by his position in the ArrayList
61		public UserDetails this[int index]
62		{
63			get
64			{
65				if (this.marrUsers.Count > index) //Position exists
66					return (UserDetails)this.marrUsers[index];
67				else
68					throw new ArgumentException("Max bounds of array exceeded");
69			}
70			set
71			{
72				if (this.marrUsers.Count > index) //Position exists
73					this.marrUsers[index] = value;
74				else
75					throw new ArgumentException("Max bounds of array exceeded");
76			}
77		}
78
79		//Gets or sets the user by email address
80		public UserDetails this[string EmailAddress]
81		{
82			get
83			{
84				//Search for user
85				for (int i = 0; i < this.marrUsers.Count; i++)
86				{
87					//Return user
88					if (((UserDetails)this.marrUsers[i]).EmailAddress.ToUpper() == EmailAddress.ToUpper())
89						return (UserDetails)this.marrUsers[i];
90				}
91				//User not found, throw exception
92				throw new ArgumentException("User not found");
93			}
94			set
95			{
96				//Search for user
97				for (int i = 0; i < this.marrUsers.Count; i++)
98				{
99					if (((UserDetails)this.marrUsers[i]).EmailAddress.ToUpper() == EmailAddress.ToUpper())
100					{
101						//Update user details
102						this.marrUsers[i] = value;
103						return;
104					}
105				}
106				//User not found, throw exception
107				throw new ArgumentException("User not found");
108			}
109		}
110	}
111}