Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on Choosing a Collection Type

Status
Not open for further replies.

blairacuda

Technical User
Sep 3, 2009
51
US
Hello All,

I need a little assistance in selecting a collection type.

This is some sample data I need to store and search easily:

Variable, Value, Boolean
Straight, 2, NO
Length, 17, YES
Connection, RAW, NO

I am leaning towards NameValueCollection because it seems to be pretty easy to add records to my keys which would be strings. But I am reading it has a lot of overhead and HashTable would be better. However I can't tell if a HashTable will allow me to have multiple records for a key.

Which of these two would be better? Or is there is a third I should be looking at?

Thanks in advance.
CBlair

Chris Blair
Crystal, InstallShield, branching out in other programming realms.
 
no collection type allows for 3 attributes. your choices are
1. a collection of objects
2. a dictionary of objects

collections are arrays, lists, sets, etc.
dictionaries are key value pairs. (dictionary, typed dictionary, hashtable ,etc).

to solve the problem with one of these enumerations you will need to create a POCO to contain the data. for example if you use a list
Code:
class Foo
{
   public string Variable {get;set;}
   public object Value {get;set;}
   public bool Usable {get;set;}
}

var foos = new List<Foo> {
   new Foo {
        Variable = "Straight",
        Value = 2,
        Usable = false
   },
   new Foo {
        Variable = "Length",
        Value = 17,
        Usable = true
   },
   new Foo {
        Variable = "Connection",
        Value = "RAW",
        Usable = false
   }
};
or a dictionary
Code:
class Foo
{
   public object Value {get;set;}
   public bool Usable {get;set;}
}

var foos = new Dictionary<string, Foo> {
   {
       "Straight", 
       new Foo {Value = 2, Usable = false}
   },
   {
       "Length", 
       new Foo {Value = 17, Usable = true}
   },
   {
       "Connection", 
       new Foo {Value = "RAW", Usable = false}
   }
if you are using .Net 4.0 you may also what to take a look at Tuple<,,> and dynamic objects as well. this would reduce some of the ceremony required by 3.5

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
So I'm essentially creating my own container? I haven't seen POCO before. What does that stand for?

Chris Blair
Crystal, InstallShield, branching out in other programming realms.
 
Plain Old Compiled Object.

Yes, you are creating a data transfer object that explicitly expressing the information that it contains.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Will I have to write all my own methods for the POCO object? Like Count() or ToString()?

Chris Blair
Crystal, InstallShield, branching out in other programming realms.
 
Count is part of the collection interface, as the POCO stands now it doesn't make sense to have a Count() method.

by default ToString() returns the full objects name name.space.classname you can override the ToString propety of your class to return whatever string you want. this is handy for value objects
Code:
class Address
{
   public string Line1 {get;set;}
   public string Line2 {get;set;}
   public string City {get;set;}
   public string State {get;set;}
   public string ZipCode {get;set;}

   public override string ToString()
   {
       return string.Format("{0}\n{1}\n{2}, {3} {4}", Line1, Line2, City, State, ZipCode);
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top