0

Hello I need to take string item from string list and a add it to the int array i trying like this but its writing me error Cannot implicity convert type int to int[] but in for sequence i adding only one int to one item in array so what is wrong please

private void buttonGenerateO_Click(object sender, EventArgs e)
            {
            List<string> AgentNumbers = new List<string>();
            List<string> AgentRide = new List<string>();
            int pocet = AgentNumbers.Count;
            int[][] dvouRozmernePole = new int[2][];
            dvouRozmernePole[0] = new int[pocet];
            dvouRozmernePole[1] = new int[pocet];

        foreach (string AgeNumb in AgentNumbers)
        {
            for (int i = 0; i < dvouRozmernePole[0].Length; i++)
                dvouRozmernePole[i] = Convert.ToInt32(AgeNumb);

        }

        foreach (string AgeRide in AgentRide)
        {
            for (int i = 0; i < dvouRozmernePole[1].Length; i++)
                dvouRozmernePole[i] = Convert.ToInt32(AgeRide);

        }

4 Answers 4

2

Look at this declaration:

int[][] dvouRozmernePole = new int[2][];

So dvouRozmernePole is an array of arrays. Now look here:

dvouRozmernePole[i] = Convert.ToInt32(AgeNumb);

You're trying to assign an int value to dvouRozmernePole[i], which you can't do because it should be an int[].

I suspect you want:

dvouRozmernePole[0][i] = Convert.ToInt32(AgeNumb);

Having said that, given these lines:

List<string> AgentNumbers = new List<string>();
List<string> AgentRide = new List<string>();
int pocet = AgentNumbers.Count;
int[][] dvouRozmernePole = new int[2][];
dvouRozmernePole[0] = new int[pocet];
dvouRozmernePole[1] = new int[pocet];

... you'll always have empty lists anyway, so there are no values to convert.

Do you really need to use arrays at all? Using List<T> everywhere would almost certainly be simpler.

LINQ can also make this simpler. For example:

int[] numbers = AgentNumbers.Select(x => int.Parse(x)).ToArray();
int[] rides = AgentRide.Select(x => int.Parse(x)).ToArray();
int[][] dvouRozmernePole = { numbers, rides };
Sign up to request clarification or add additional context in comments.

Comments

0

dvouRozmernePole is an array of int[], while Convert.ToInt32 returns an int. Therefore in:

dvouRozmernePole[i] = Convert.ToInt32(AgeNumb);

dvouRozmernePole[i] is an int[], and you are trying to assign an int.

It looks like you want:

dvouRozmernePole[0][i] = Convert.ToInt32(AgeNumb);

and later

dvouRozmernePole[1][i] = Convert.ToInt32(AgeRide);

You could instead do:

int[][] dvouRozmernePole = new int[2][]
{
    AgentNumbers.Select(num => Convert.ToInt32(num)).ToArray(),
    AgentRide.Select(ride => Convert.ToInt32(ride)).ToArray()
};

to initialise the array.

2 Comments

Ok im quite a noob so how can i add items from list to array ? Im googling for 3 hours and this is a result but now im stacked... how can i fill the array ?
@user1936559 - There are a few ways, one is a loop similar to what you have, another is to use LINQ. I've added a LINQ example.
0

The error is quite clear. In the statement

dvouRozmernePole[i] = Convert.ToInt32(AgeNumb);

you assign an integer to an array.

Comments

0

dvouRozmernePole is an array of array of ints

so dvouRozmernePole[i] expects and int[] rather than the int value

try

did you mean

    for (int i = 0; i < dvouRozmernePole[0].Length; i++)
           dvouRozmernePole[0][i] = Convert.ToInt32(AgeNumb);
    }

    foreach (string AgeRide in AgentRide)
    {
        for (int i = 0; i < dvouRozmernePole[1].Length; i++)
            dvouRozmernePole[1][i] = Convert.ToInt32(AgeRide);

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.