which collection to use

  • Thread starter Thread starter Saso Zagoranski
  • Start date Start date
S

Saso Zagoranski

Hi!

I need an array-type of collection when the entries would look like:
object myObject, int myInt, string myString

Which collection should I use?
I know I could declare a multidimensional array of objects but adding and
deleting to that
array would require distribution of objects each time something was deleted
or added so
that doesn't feel like the correct approach.

Thanks,
saso
 
Hi Saso,

One suggestion would be to create a custom type,
(say something like the MyEntry class shown below)
that contains the fields (In your example -
object myObject, int myInt, string myString)
that comprise the entries to be added to the collection.

You can then add/remove instances of the MyEntry class
from a collection such as the ArrayList.

public class MyEntry
{
private object _myObject;
private int _myInt;
private string _myString;

// Expose a few public properties here to
// access/modify the private fields
// ...
}

ArrayList list = new ArrayList();
list.Add(new MyEntry(myObj, myInt, myString));

You could also create a strongly typed collection
class derived from CollectionBase that could allow you to
add and remove instances of MyEntry.

Regards,
Aravind C
 
There are many types of collections. Even specialized ones. Could you
provide us with some more information like what is needed : fast adding,
fast removing, strict order, looking up if an item is in the collection
based on one of the fields you specified (which one), ...
Each collection type has it's own benefits.

Yves
 
I need adding, removing and searching. Nothing special really... No real
speed
requirements either.

Aravind's idea seem ok... what do you think?
 
I would create a class with the elements in it. If you would search on the
string you might want to check out the StringDictionary class. Otherwise
you're best of using an ArrayList.

Yves
 
Back
Top