C# Programming
08-26-2009, 01:41 AM
I have a class which has a dictionary and I don't want to reveal this information to the outside. So I simply made it a private member. I want people to enumerate through my class so I did the following:
public class CustomerCollection : IEnumerable
{
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return this._districts.GetEnumerator();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return this._districts.GetEnumerator();
}
#endregion
}
This works fine but during enumeration, the client has to write code as below:
foreach (KeyValuePair c in this._customers) // _customers is an instance of CustomerCollection
{
c.Value.Name = "Whatever";
}
What should I do if I want the client to be able to do this:
foreach (Customer c in this._customers)
{
c.Name = "whatever";
}
CodingYoshi
Artificial Intelligence is no match for Human Stupidity.
public class CustomerCollection : IEnumerable
{
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return this._districts.GetEnumerator();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return this._districts.GetEnumerator();
}
#endregion
}
This works fine but during enumeration, the client has to write code as below:
foreach (KeyValuePair c in this._customers) // _customers is an instance of CustomerCollection
{
c.Value.Name = "Whatever";
}
What should I do if I want the client to be able to do this:
foreach (Customer c in this._customers)
{
c.Name = "whatever";
}
CodingYoshi
Artificial Intelligence is no match for Human Stupidity.