views:

930

answers:

4

I'm using a datatable where for each row I want to store some 3 or 4 fields, the fields are of different types, best case number of fields is 3, and I want to note the row_index (of the row in the datatable) as I need it.

So like this I want to store the values (for the fields) for all the rows in the datatable.

Please give me an example, code snippet, or more information about how to structure/access this information for any nth row with the row index (which is also stored)

+3  A: 

Arrays must be assigned a length, to allow for any number of elements: use the List class.

For example:

List<int> myInts = new List<int>();
myInts.Add(5);
myInts.Add(10);
myInts.Add(11);
myInts.Count // = 3

To store many values inside of the list, I would suggest creating your own class and storing that. But if you don't want to, you can have a list of lists of objects.

List<List<object>> myList = new List<List<object>();
myList.Add(new List<object>() { 1, "APPLE", "red", "sweet" } );
Samuel
And he said array, so I doubt he wants something that does more than what an array could (outside of unknown length). Now, that's not to say he might not need something better.
Samuel
using object is not such great advice; kind of defeats the purpose of using a strongly typed list!
Mitch Wheat
And ArrayList or OrderedDictionary are strongly typed?
Samuel
I can see several problems with this answer: 1) as Mitch said, you can't access individual elements by their index (you would have to iterate through if I am not mistaken?). 2) This doesn't the part of the question concerning each element having an indefinite amount of "values" or "properties"
TheTXI
Samuel
@Samuel: based on the question I am guessing 1 and 4 wouldn't be indexes only because of the way he says Element[0] and Element[1]. I would imagine the 1 and 4 are some other type of property (perhaps something like a FruitID?)
TheTXI
@Mitch: Not sure where you are getting that, but System.Collections.Specialized.OrderedDictionary isn't generic.
Samuel
Excuse my earlier brain fart, it is possible to access items in a List(of T) by index. Apparently my mind regressed momentarily back to linked lists in C++
TheTXI
@Samuel: apologies Samuel; you are correct.
Mitch Wheat
+1  A: 

If you have absolutely no idea what fields can be in each element, your only choice is to create a property bag (map/dictionary) of key value pairs.

You can use an ArrayList or an OrderedDictionary, which allow indexing by ordinal, to hold each property bag element.

Mitch Wheat
A: 

You can use XMLDocument to store data. It is flexible enough to store and manipulate data even if you only need to use it in memory.

Sergio
A: 

Hi,

I'm using a datatable where for each row i want to store some 3 or 4 fields...

fields are of different types

(best case:: no:of fields is 3) and i want to note the row_index (of the row in the datatable) as i need it.

so like this i want to store the values(for the fields) for all the rows in the datatable...

Pls give me an e.g (of this scenario)//code snippet// and how to access the fields for any ith row with the row index(which's also stored)

stack_pointer is EXTINCT
For future reference, please make any updates to your original question in the original question using the Edit function. This space is reserved for answers (I don't want you getting voted down just because you are not familiar with SO).
TheTXI