Re: ArrayList grouping question
From: Simon Smith (ghytred_at_community.com)
Date: 06/21/04
- Next message: Morten Wennevik: "Re: Click buffer"
- Previous message: Morten Wennevik: "Re: How to get user company"
- Maybe in reply to: Hans De Schrijver: "ArrayList grouping question"
- Messages sorted by: [ date ] [ thread ]
Date: 21 Jun 2004 07:04:36 +0100
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
On 20 Jun 2004 19:56, "Hans De Schrijver" wrote:
>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" <ghytred@community.com> wrote in message
>news:6a10672c8a6544379619ca9d13bf677e@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
>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
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>>
>>
>>
>
>
>
>
>
>
>
- Next message: Morten Wennevik: "Re: Click buffer"
- Previous message: Morten Wennevik: "Re: How to get user company"
- Maybe in reply to: Hans De Schrijver: "ArrayList grouping question"
- Messages sorted by: [ date ] [ thread ]