ArrayList grouping question

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
 
S

Simon Smith

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
 
H

Hans De Schrijver

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 said:
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

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
 
J

Jeff Louie

Hans... Off the top of my pointed head, the cost of efficiency would be
the
added complexity of maintaining an index of managers in the arraylist.

Regards,
Jeff

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
S

Simon Smith

Hi Hans -

Well, your Add (and presumably Remove) method is on your own Class1. So
in the Add and Remove methods of Class1 you work out what is being added/removed
and work accordingly:
// Class1
public void Add(User user) {
// add to all users
allUsers.Add(user);
// add to managers if appropriate
Manager m = user as Manager;
if (m != null) {
managers.Add(m);
}
}

And similar in the Remove.

A better solution might (just might - depends) be to create your own Collection
class inheriting from CollectionBase, and similarly implementing the Add/Remove
methods and adding the Managers method. Depends on what else Class1 is
doing.



Simon Smith
simon dot s at ghytred dot com
www.ghytred.com/NewsLook - NNTP Client for Outlook

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 said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top