CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: String array

  1. #1
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    971

    String array

    Code:
                        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?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: String array

    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:

    Code:
    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.

  3. #3
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    971

    Re: String array

    Thanks for the reply.
    My keyborard is stuffed.sorry about the CAPS.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: String array

    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'.

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: String array

    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[].
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured