简体   繁体   中英

Passing arrays to methods

Even though all variables are arrays I seem to be getting errors stating that conversion from string to string[] variables is not possible.

class Program
{

    private static int[] testKey = { 10, 12, 15, 18, 2, 1, 200 };
    private static string[] testVar1 = { "hello", "albert", "france", "john", "version", "zebra" };
    private static string[] testVar2 = { "helllllllo", "jordan", "land", "Hobart", "Hogwarts", "hamburger", "Code" };

    static void Main(string[] args)
    {
        for (int currentLineCount = 0; currentLineCount < 233; currentLineCount++)
        {
            OrderAZ(testKey[currentLineCount], testVar1[currentLineCount], testVar2[currentLineCount]);
        }
        //Argument 3: cannot convert from 'string' to 'string[]'
        //Argument 2: cannot convert from 'string' to 'string[]'
        //Argument 1: cannot convert from 'int' to 'int[]'
    }

    public static void OrderAZ(int[] sortKey, string[] sortVariableTwo, string[] sortVariableOne)
    {
       //sort method stub
    }
}

testKey is an array of int , but testKey[currentLineCount] is an int . The OrderAZ method expects a int[] , not an int (and similarly for the other parameters). You need to either pass an array, or change the signature of your OrderAZ method to accept an int (hard to tell without knowing what your code is supposed to do).

if you want send a string as an array you may need to use a string method to convert it in to a string array such a string.split() , a string is not an array of strings as a int is not an array of int till you convert them in to it somehow.

In this case you are sending ítems of the array and an ítem of an array of strings is actually a string, so your method has to recieve a string, but if you want to send the whole array then remove the whole brackets part on the call, for example:

OrderAZ(testKey, testVar1, ...);

Then you can keep your method as it is. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM