What type of collection of objects to use?

Now I have an array of 2d strings containing my data:

//...find number of rows needed... string[,] data = new string[totalRows, 12]; //row / column 

It works. BUT now, when I want to add more functionality to my program, this is no longer in my interests, since concatenating 2-dimensional arrays, while executable, opens up other problems: until that moment, I stored the number of lines in a class variable, so as I needed to support two at once. Perhaps I know that the columns will always be the same, and I could divide the length by getting the rows and writing a method to combine them.

I get the feeling that there is a better way to do this. My knowledge of the β€œnew” things is missing, but I am sure that one of them is better suited for the bill than the others. Therefore, before I jump in and create a List<List<String>> data = new List<List<String>>(); or something else as strange to watch, I want the opinions of others to be more experienced.

I do not need the functions of sorting, deleting, pasting, etc. I just need to store data; and now I need to be able to relatively easily switch data += data2 - something to this effect, one way or another. data.Length (giving only the outer length) is also very useful.

What is the best way to do this?

Please let me know if you would like more information. Thanks.

Additional information based on answers:

The data is mainly a spreadsheet format. i.e.

 [['1234-56789-12345', 'screwdriver', '', 'ea', '1', '', '', '', '', '', '', ''], [['1234-56789-54321', 'wrench', '', 'ea', '1', '2', '3', '', '', '', '', '']] 

I don't want to deal with figuring out what the information describes - I don't care. I need a place in relation to everything else.

Additional Information:

Use - as a storage tank between one xml file and another. I just realized that these xlst things might be another solution to my original problem, but, yes. Maybe in a different life. (Everything works .. as I said, adding functionality. If it works, why break it?)

+4
source share
4 answers

It depends on how rigid or flexible the "12" is.

I would say List<List<string>> is a flexible approach.

And List<string[]> will fix it (it might be easier with zero columns), while still being flexible regarding the number of rows. As a jagged array, you need to configure it with a for loop:

 List<string[]> data = new List<string[]>(); for (int i = 0; i < desiredRows; i++) data.Add(new string[12]); data[1][1] = "Screwdriver"; 
+1
source

I think a question worth asking might be better if your data columns are represented by an object that explains what each column holds. For example, instead of

 row 1 => { "Jane", "Smith", "1 Rocket Ave", "Houston", "TX" } 

You have

 new Person { FirstName = "Jane", LastName = "Smith", /* etc. */ } 

So, your 2D array will become a one-dimensional collection of these new Person objects that are easier to reason in code.

 List<Person> people = ... // vs. string[,] people = ... 
+2
source

Why don't you use Dictionary ?

Examples here .

0
source
 Dictionary<string,List<string>> 

or

 Dictionary<int,List<string>> 

or

 Dictionary<int,string[]> 

and etc.

This will depend on what type of key you want to use (strings for names, int for identifiers, etc.)

If you do not need to search for some key, there is nothing wrong with List<List<string>> .

0
source

All Articles