473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

ArrayList grouping question

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
Nov 16 '05 #1
4 1707
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



Nov 16 '05 #2
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



Nov 16 '05 #3
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!
Nov 16 '05 #4
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" <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 theobjects >of the type corresponding to the property. Problem is, this hapens every
>time you access the property, and the idea is that these properties willbe >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 missingsome >functionality of the ArrayList object that could simplify what I want todo? >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 ArrayListat >any time before or after using the public accessor properties.
>
>Thanks in advance for any suggestions.
>
>-- Hans De Schrijver
>
>
>
>
>
>
>





Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andreas Håkansson | last post by:
Seeing how my previous post seem to have fallen between the cracks, I thought I would have a second, more direct, go at it. So my question is "Is it possible to group (Muenchian method) over...
7
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a...
3
by: george r smith | last post by:
I am trying to create an arrayList that contains multiple arrayLists. My code attempt is below. The question I have is how can I get away from creating another pAttribute list than can be added to...
12
by: Laser Lu | last post by:
Hello, everybody, do you know how to use this Grouping Construct? (?> ) I've found its reference on MSDN, but still can not understand it totally. The following is its description: ...
6
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the problem solving statement is necessary. The inspiration for the statement came from an undocumented VB...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
3
by: Mark Jones | last post by:
I am quite new to ASP and .Net development and I am building a web-based multiple choice exam application. The web page displays the questions using a Repeater control and the answers are nested...
31
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello My pgm1 (User Interface Level) passes an empty ArrayList to pgm2 (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate the ArrayList. Question1: When pgm2 gets...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.