CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2003
    Location
    Corvallis, OR
    Posts
    315

    Array of string pairs

    I need to create an array that will hold an uknown (at compile time) number of string pairs.

    [0] = "pair1 first string", "pair1 second string"
    [1] = "pair2 first string", "pair2 second string"
    [2] = "pair3 first string", "pair3 second string"
    [3] = "pair4 first string", "pair4 second string"

    I think this is a 3-dimensional array, but I'm not sure.

    The array def will be passed into a fxn by ref (with an initial value of null). The function should then fill the array to look similar to what you see above.

    Can someone help me implement this. Thanks!

    ps.. No, this is not a homework assignment. Its for a webservice that I am writing whereby the web page passes the null array to the webservice and receives back the data.

  2. #2
    Join Date
    Oct 2006
    Location
    Timisoara, Romania
    Posts
    123

    Re: Array of string pairs

    It is a 2-dimensional array.

    You can implement it as
    Code:
    string[,] Strings; // declaration
    ...
    int n; // number of elements unknown at compile-time
    ... 
    // find out n
    ...
    Strings = new string[n,2]; // instantiate array
    You can access the elements of the string with Strings[i][0] (first string at index i) and Strings[i][1] (second string at index i).

  3. #3
    Join Date
    May 2003
    Location
    Corvallis, OR
    Posts
    315

    Re: Array of string pairs

    Getting an error...
    Code:
    string [,] aryKeys = new string[10, 2];
    int nKeysGenerated = 0;
    
    //error: CS0022: Wrong number of indices inside [], expected '2'
    aryKeys[nKeysGenerated][0] = "key1 first string";
    aryKeys[nKeysGenerated++][1] = "key1 second string";
    
    aryKeys[nKeysGenerated][0] = "key2 first string";
    aryKeys[nKeysGenerated++][1] = "key2 second string";
    And how do I pass a 2-dim array of uknown size by ref?

    Code:
    int nKeysGenerated = 0;
    string[,] aryKeys;
    /error: Argument '1': cannot convert from 'ref string[*,*]' to 'ref object'
    if (GetKeys(ref aryKeys, out nKeysGenerated))
    {                
        for (int nCur = 0; nCur < nKeysGenerated; nCur++)
        {
            lstKeys.Items.Add(string.Format("{0},{1}", aryKeys[nCur, 0], aryKeys[nCur, 1]));
        }
            
    }
    
    //and the fxn def
        public bool GetKeys(ref string[,] aryKeys, out int nKeysGenerated)

  4. #4
    Join Date
    May 2003
    Location
    Corvallis, OR
    Posts
    315

    Re: Array of string pairs

    I found out that you can't pass a multi-dimensional array to a webservice. So I just passed 2 single dimensional arrays. Messy, but it works.

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