Click to See Complete Forum and Search --> : String array


Saeed
March 23rd, 2009, 11:55 PM
string[] str_a = new string[]; // COMPLIER DOES NOT LIKE THIS
string[] str_a = new string[256]; // COMPLIER wants THIS

int col=0;
while ( BLAA BLAA BLAA))
{
str_a[col++] = get(...);
}



return str_a; // but the size is of the array is 256 not value of col


I DONT WANT TO SET MY STRING [] TO HAVE 256.
one WAY I READ WAS TO USE LIST<LIST<string>> but i still try not to do that.

Final question is this :
can teh size of the array be altered from 256 to what the value of COL is somehow before being returned back?

BigEd781
March 24th, 2009, 12:12 AM
Why all of the alternated CAPS?

Like you read, use a generic list. It is made to be used as a dynamically sized array, so use it:


List<string> strings = new List<string>();
strings.Add("one");
strings.Add("two");
strings.Add("three");

And if you need o work with it as an array at some point, there is always the ToArray() method.

Saeed
March 24th, 2009, 12:21 AM
Thanks for the reply.
My keyborard is stuffed.sorry about the CAPS.

BigEd781
March 24th, 2009, 01:23 AM
Honestly, without knowing the "blah blah blah" part of that loop, it is hard to give you any more advice. You are just looping through an array, but you don't show us how you are obtaining the value of col. At what point does the loop terminate? Can't you just the test value before entering the loop? You could then declare the array after setting 'col'.

boudino
March 24th, 2009, 04:14 AM
If you don't know the size of array before you allocate it, use List<string> to accomodate the uknown number of items and than call ToArray() on it to get an instance of string[].