H
Hans De Schrijver
I have a private ArrayList variable that holds objects of various types,
though they're all derived from a common base class (User).
What I would like to do is provide public accessor properties per type. I
have written some code that does the trick now, but it involves looping
through the private ArrayList and creating a new Array with just the objects
of the type corresponding to the property. Problem is, this hapens every
time you access the property, and the idea is that these properties will be
used in a loop like foreach(Manager m in oClass1.Managers).
Below is the simplified sample code:
public class Class1
{
ArrayList allUsers;
public Manager[] Managers
{
get
{
ArrayList managers = new ArrayList();
foreach(User u in this.allUsers)
{
if (u Is Manager)
managers.Add(u);
}
return (Manager[]) managers.ToArray(typeof(Manager));
}
}
public Worker[] Workers
{
get
{
ArrayList workers = new ArrayList();
foreach(User u in this.allUsers)
{
if (u Is Worker)
workers.Add(u);
}
return (Worker[]) workers.ToArray(typeof(Worker));
}
}
}
Does anyone have any suggestinos on how to improve this? Am I missing some
functionality of the ArrayList object that could simplify what I want to do?
Or is there a better appraoch to this than ArrayLists? Keep in mind that
objects can be added to and removed from the private allUsers ArrayList at
any time before or after using the public accessor properties.
Thanks in advance for any suggestions.
-- Hans De Schrijver
though they're all derived from a common base class (User).
What I would like to do is provide public accessor properties per type. I
have written some code that does the trick now, but it involves looping
through the private ArrayList and creating a new Array with just the objects
of the type corresponding to the property. Problem is, this hapens every
time you access the property, and the idea is that these properties will be
used in a loop like foreach(Manager m in oClass1.Managers).
Below is the simplified sample code:
public class Class1
{
ArrayList allUsers;
public Manager[] Managers
{
get
{
ArrayList managers = new ArrayList();
foreach(User u in this.allUsers)
{
if (u Is Manager)
managers.Add(u);
}
return (Manager[]) managers.ToArray(typeof(Manager));
}
}
public Worker[] Workers
{
get
{
ArrayList workers = new ArrayList();
foreach(User u in this.allUsers)
{
if (u Is Worker)
workers.Add(u);
}
return (Worker[]) workers.ToArray(typeof(Worker));
}
}
}
Does anyone have any suggestinos on how to improve this? Am I missing some
functionality of the ArrayList object that could simplify what I want to do?
Or is there a better appraoch to this than ArrayLists? Keep in mind that
objects can be added to and removed from the private allUsers ArrayList at
any time before or after using the public accessor properties.
Thanks in advance for any suggestions.
-- Hans De Schrijver