C# declare empty string[ ][ ] (array of arrays)

leorob88

Well-known member
Joined
Dec 31, 2020
Messages
56
Programming Experience
1-3
Hello there, my question is simple as that. I need to declare an array of arrays which are all strings, because I need to creare some sort of "grid". I'm importing a text file with a variable number of rows, but each row will have 4 values separated by ||, (so my idea would be to use a text split to eliminate the NewLines and then another split to eliminate the ||). When imported, the idea would be to set each value of each row into a specific position of the array[ ][ ] so then I can be able to sort each row of the grid based on two possible columns (the columns are 4).

I found online if I use Linq I can use something like "OrderBy(entry => entry[0]).ToArray( )" to sort the grid supposedly based on the first column, but my problem actually is to create a string[ ][ ] which is empty and then I can populate with variable content (remember, I don't know how many rows I have, I also tried but failed to set a basic static size of 100 rows). Visual Studio doesn't exactly prompt errors in the code but instead on the execution. Meaning, I tried different things, first of all array.Append(string1, string2, string3, string4), which would be the 4 values of a row, assuming Append just inserts a string[ ] inside the string[ ][ ] (which should be empty), but when I test it with a MessageBox, recalling the value in array[0][1] it just doesn't work, saying I'm off the limits of the array.

Do you have any suggestions about this? I'm sure I'm going around the same error and I'm missing something basic, but searching online I couln't find anything about this specific case because I find only how to create string[ , ] (jagged arrays, right?) which don't let me use the OrderBy or else nothing about empty string[ ][ ] so I'm a bit stuck...
 
A list of array of strings sounds like a better fit given that you said that you have a variable number of rows, but each row will be exactly 4 columns.

You could still work with your array of array of strings, but you'll need to know number of rows ahead of time to allocate the outermost dimension. It also looks like you just glossed over learning about how references work in C# and how they work in conjunction with arrays. Take time to review that because you can allocate the outermost dimension, and then just take the results of Split() to assign the values.

Anyway, my recommendation is to stop thinking of the data as a grid of strings, but as a collection of objects/entities. Read a line, and instantiate an object that has fields initialized from that line. Put that instance into some kind of collection -- most likely a list. After loading everything in, then sort the collection.
 
A list of array of strings sounds like a better fit given that you said that you have a variable number of rows, but each row will be exactly 4 columns.

You could still work with your array of array of strings, but you'll need to know number of rows ahead of time to allocate the outermost dimension. It also looks like you just glossed over learning about how references work in C# and how they work in conjunction with arrays. Take time to review that because you can allocate the outermost dimension, and then just take the results of Split() to assign the values.

Anyway, my recommendation is to stop thinking of the data as a grid of strings, but as a collection of objects/entities. Read a line, and instantiate an object that has fields initialized from that line. Put that instance into some kind of collection -- most likely a list. After loading everything in, then sort the collection.
Nice idea, I didn't consider the List! I'll try it right away! Also, if this works, it will be like "again, they helped me here and StackOverflow wasn't able to help me at all!", which is fine since you helped me a lot also in the past!
 
A list of array of strings sounds like a better fit given that you said that you have a variable number of rows, but each row will be exactly 4 columns.

You could still work with your array of array of strings, but you'll need to know number of rows ahead of time to allocate the outermost dimension. It also looks like you just glossed over learning about how references work in C# and how they work in conjunction with arrays. Take time to review that because you can allocate the outermost dimension, and then just take the results of Split() to assign the values.

Anyway, my recommendation is to stop thinking of the data as a grid of strings, but as a collection of objects/entities. Read a line, and instantiate an object that has fields initialized from that line. Put that instance into some kind of collection -- most likely a list. After loading everything in, then sort the collection.
Ok no, I'm already stuck now that I'm trying and the matter is simple: if I want to add something to a List, I must know what I'm adding, but that is (in my case) a cyclical number of elements.

I mean how can I have a Z number cycle (for int Z=....) and say List.Add(Zarray)? I should create a single string[ ] for each row of the text, and then add that array to the List. Meaning, I should know how many arrays I need in the code and create or delete each time some arrays to have the exact number of arrays I need. I'd need a more flexible solution...and I suppose it can be done in a simple way, no?

Also, I tried just to create "many" string[ ] to put in the List and it "kinda" works, but actually it only does work when it's based on words. Meaning, if I want to order the rows based on a number it doesn't sort the list at all (a column is a number of "votes" and another is a DateTime value, I assumed it just could handle numeric values since they're still strings but it seems to me it's more based on an "alphabetical" format, i.e. numbers are not letters and it doesn't work. Don't get me wrong, I can just get around this with a long and boring script, creating a cyclilcal function that check cyclically every value to insert in the list the "next" right value based on some parameters I give to the cycle, but still it seems strange to me it has to be so long of a work for a task conceptually so simple...but whatever, the result is what matters I suppose!
 
Last edited:
Yes, it can be done simply. Breakdown the big problem into smaller problems. Solve the smaller problems. Piece together the smaller solutions to solve the big problem.

You are letting yourself get overwhelmed by trying to keep all the pieces in your head. Over time, you'll develop the skill like a chess player can keep multiple branching chess moves visualized, but until then use pen and paper, or a whiteboard to draw out your plans.

I usually discourage designing at the keyboard, but it does work for some people as they organically come up with a design. Those people usually already have a general plan or a design pattern in mind, and they just iteratively narrow down the details of the implementation.
 
Last edited:
Those people usually already have a general plan or a design pattern in mind
Yeah, actually that's what I usually do, because first I have the idea of what to do, then, when I have the concept in my mind I start creating it. My problem is exactly some rare times my concepts are simple, but they're not so simple to create.

For now, since I couldn't come up with anything better, I just created a series of variables and then put them into a List one by one. Which means I literally script each single string[ ] to contain something and to be put in the List and I will have to create a "for" cycle which literally controls each step with If instances. Meaning, suppose the cycle goes from 10 to 1, inside the cycle there will be "if cycle 10, handle string10, if cycle 9, handle string9, and such, until 1; it's dumb I know, but right now I couldn't do in any smart way what I wanted to do.
 

Latest posts

Back
Top Bottom