locked
How to pass Array values as parameter RRS feed

  • Question

  • Hi expert, 

    I need your help, the below the what i coded, and the error is what descriptive.

    So how to pass the array like in the code below?

     string[] strArrOne = new string[3];
     string[] strArrTwo = new string[3];
     string[] strArrThree = new string[3];
     
     for (int intArr= 0; intArr < 3; intArr++)
     {
            MethodArray (strArrOne[intArr], strArrTwo[intArr], strArrThree[intArr], intArr );
     }
    
      public void MethodArray(ref string[] strArrOne, ref string[] strArrTwo,  ref string[] strArrThree,  int intIndex)
     {
           strArrOne[intIndex] = "Test";
           strArrTwo[intIndex] = "Test";
           strArrThree[intIndex] = "Test";
     }


    Error:

    Error      1              MethodArray(ref string[], ref string[], ref string[], int)' has some invalid arguments

    Error      2              Argument 1: cannot convert from 'string' to 'ref string[]'               

    Error      3              Argument 2: cannot convert from 'string' to 'ref string[]'               

    Error      4              Argument 3: cannot convert from 'string' to 'ref string[]'                

    Thanks in advance...

    Sunday, May 8, 2016 7:04 AM

Answers

  • Your code is all kinds of messed up. You should maybe go a few steps back and tell us what this is supposed to do.

    I can only tell you what all is wrong:

    - If you have a ref or out parameter you must aknowledge that you understand it is ref/out, by writing it when calling the function too.

    MethodArray (ref strArrOne[intArr], ref strArrTwo[intArr], ref strArrThree[intArr], intArr );

    But I don't think ref is the right thing here either way.

    - If you hand in the n-th Element of a String Array, you hand in a single string, not the entire array. So naturally you can not hand a single string to a function that expect an array of strings at that place.

    - Then there is the part where the for loop is just inside the class. It has to be inside a function - it is not a peer of the variables.
    Only variables and functions are peers of the each other. And the for loop must be a child of a function.

    Again there are at least 3 different things I think you might have wanted to do. And I can't nail down wich one it is. Or even when and how this code should be called, or by whom.

    • Proposed as answer by DotNet Wang Monday, May 9, 2016 7:37 AM
    • Marked as answer by DotNet Wang Monday, May 16, 2016 12:40 PM
    Sunday, May 8, 2016 8:43 AM

All replies

  • Your code is all kinds of messed up. You should maybe go a few steps back and tell us what this is supposed to do.

    I can only tell you what all is wrong:

    - If you have a ref or out parameter you must aknowledge that you understand it is ref/out, by writing it when calling the function too.

    MethodArray (ref strArrOne[intArr], ref strArrTwo[intArr], ref strArrThree[intArr], intArr );

    But I don't think ref is the right thing here either way.

    - If you hand in the n-th Element of a String Array, you hand in a single string, not the entire array. So naturally you can not hand a single string to a function that expect an array of strings at that place.

    - Then there is the part where the for loop is just inside the class. It has to be inside a function - it is not a peer of the variables.
    Only variables and functions are peers of the each other. And the for loop must be a child of a function.

    Again there are at least 3 different things I think you might have wanted to do. And I can't nail down wich one it is. Or even when and how this code should be called, or by whom.

    • Proposed as answer by DotNet Wang Monday, May 9, 2016 7:37 AM
    • Marked as answer by DotNet Wang Monday, May 16, 2016 12:40 PM
    Sunday, May 8, 2016 8:43 AM
  • Thanks for your reply, it is very helpful...

    :-)



    • Edited by cahmad Sunday, May 8, 2016 9:12 AM
    Sunday, May 8, 2016 9:11 AM