Hey Simon,
I thought of separate arrays too. However, how can I intercept the various
Add and Remove commands that can be executed on the private ArrayList
through the public accessor properties? Because right now I can't add or
remove objects from the private allUsers ArrayList from outside the class.
If I could intercept these Add and Remove commands, I could properly handle
adding and removing objects to the private allUsers array.
Summarized, here's the problem, using the earlier post below as a class
reference.
public class Tester
{
static void Main
{
Class1 oClass1 = new Class1();
Manager oManager = new Manager();
Worker oWorker = new Worker();
oClass1.Managers.Add(oManager);
oClass1.Workers.Add(oWorker);
}
}
This code won't work correctly because it will attempt to add oManager and
oWorker to a temporary arrayList that doesn't really exist.
Strangely enough, the code will compile and run without errors, but contrary
to what I would have expected, adding Console.WriteLine("Workers: {0},
Managers: {1}", oClass1.Workers.Count, oClass1.Managers.Count) at the end
displays 0, 0, revealing that the arrayLists are read-only.
What to do???
Any help is greatly appreciated.
-- Hans De Schrijver
"Simon Smith" <gh*****@community.com> wrote in message
news:6a******************************@ghytred.com. ..
I think I'd keep two ArrayLists - one for all users, one for managers.
Add to the manager ArrayList as well as the all users list if it's a
manager. There is duplication, sure, but only of a 4-byte reference.
Otherwise, I think you're stuck with what you're doing.
Simon Smith
simon dot s at ghytred dot com
www.ghytred.com/NewsLook - NNTP Client for Outlook
On 20 Jun 2004 17:50, "Hans De Schrijver" wrote: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
objectsof the type corresponding to the property. Problem is, this hapens every
time you access the property, and the idea is that these properties will
beused 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
somefunctionality 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
atany time before or after using the public accessor properties.
Thanks in advance for any suggestions.
-- Hans De Schrijver