7

What's a good collection in C# to store the data below:

I have check boxes that bring in a subjectId, varnumber, varname, and title associated with each checkbox.

I need a collection that can be any size, something like ArrayList maybe with maybe:

      list[i][subjectid] = x;
      list[i][varnumber] = x;
      list[i][varname] = x;
      list[i][title] = x;

Any good ideas?

chris
  • 3,783
  • 3
  • 17
  • 13

8 Answers8

14

A List<Mumble> where Mumble is a little helper class that stores the properties.

List<Mumble> list = new List<Mumble>();
...
var foo = new Mumble(subjectid);
foo.varnumber = bar;
...
list.Add(foo);
,..
list[i].varname = "something else";
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
7
public Class MyFields
{
    public int SubjectID { get; set; }        
    public int VarNumber { get; set; }
    public string VarName { get; set; }
    public string Title { get; set; }
}

var myList = new List<MyFields>();

To access a member:

var myVarName = myList[i].VarName;
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
1

A generic list, List<YourClass> would be great - where YourClass has properties of subjectid, varnumber etc.

Will A
  • 24,780
  • 5
  • 50
  • 61
0

You'd likely want to use a two-dimensional array for this, and allocate positions in the second dimension of the array for each of your values. For instance, list[i][0] would be the subjectid, list[i][1] would be varnumber, and so on.

Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
  • Can the 1st dimension grow and shrink or do I have to allocate a specific size ahead of time? – chris May 17 '11 at 23:16
  • 4
    Yuck. No [magic numbers](http://en.wikipedia.org/wiki/Magic_number_(programming)). They're awful to troubleshoot six months down the road. – Michael Todd May 17 '11 at 23:17
  • This isn't a very good solution, because the indices are weakly typed. (ie you have to know that index 0 is subjectid). – Alan May 17 '11 at 23:18
  • I'm not up-to-speed on C# at the moment, but I believe you have to allocate a specific size ahead of time. There are ways of changing the length of the array, though, if you need to. – Elliot Bonneville May 17 '11 at 23:18
  • The way is looks is that a two dimensional array is not really required because the second dimension looks like properties on a class, so all the properties are related on the first dimension by group rather than being positionally related. This may also help: http://colinmackay.co.uk/blog/2010/09/09/tip-of-the-day-19-create-a-list-of-objects-instead-of-many-lists-of-values/ – Colin Mackay May 17 '11 at 23:20
  • Ah. Well, ignore me, then. Like I said, I'm definitely not up-to-speed in C#. – Elliot Bonneville May 17 '11 at 23:23
0

Determining what collection, typically begins with what do you want to do with it?

If your only criteria is it can be anysize, then I would consider List<>

Alan
  • 45,915
  • 17
  • 113
  • 134
0

Since this is a Key, Value pair I would recommend you use a generic IDictionary based collection.

// Create a new dictionary of strings, with string keys, 
// and access it through the IDictionary generic interface.
IDictionary<string, string> openWith = 
    new Dictionary<string, string>();

// Add some elements to the dictionary. There are no 
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
Frazell Thomas
  • 6,031
  • 1
  • 20
  • 21
  • dictionary won't quite work, as the subjectid, varnumbers are all tied together as a unique primary key so you can't have dictionary dictSubject with the same subject key (that is the problem I am having) – chris May 17 '11 at 23:19
  • I usually use Hans method as it allows you to strongly type the whole collection so that might be the best option. You could then use an enumeration to denote the key. – Frazell Thomas May 17 '11 at 23:22
0

As others have said, it looks like you'd be better creating a class to hold the values so that your list returns an object that contains all the data you need. While two-dimensional arrays can be useful, this doesn't look like one of those situations.

For more information about a better solution and why a two-dimensional array/list in this instance isn't a good idea you might want to read: Create a list of objects instead of many lists of values

Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
0

If there's an outside chance that the order of [i] is not in a predictable order, or possibly has gaps, but you need to use it as a key:

public class Thing
{
    int SubjectID { get; set; }        
    int VarNumber { get; set; }
    string VarName { get; set; }
    string Title { get; set; }
}

Dictionary<int, Thing> things = new Dictionary<int, Thing>();
dict.Add(i, thing);

Then to find a Thing:

var myThing = things[i];
Kev
  • 118,037
  • 53
  • 300
  • 385