Click here to Skip to main content
15,397,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a form in which, I have three listBoxes and one button. I put the integer value in listBox1 and ListBox2 dynamically(from user through textBoxes).
I want, when i click on button1 first Concatenate the two listBox items in one string array and then it sort that values, and added it to listBox3 ... (I want it without using loops)
My code is -:
using System;
using System.Windows.Forms;

namespace JustForFun
    public partial class ArrayStringToInt : Form
        public ArrayStringToInt()

        private void button1_Click(object sender, EventArgs e)
            string[] strArr = new string[listBox1.Items.Count + listBox2.Items.Count];
            int[] intArr = new int[listBox1.Items.Count + listBox2.Items.Count];
            listBox1.Items.CopyTo(strArr, 0);
            listBox2.Items.CopyTo(strArr, listBox1.Items.Count);
            System.Array.Copy(strArr, intArr, strArr.Length);//Error is here
            System.Array.Copy(intArr, strArr, intArr.Length);

When i Run this code its give an error :
Source array type cannot be assigned to destination array type.

My question is -> is it possible to do this (without using loops) ??

Any suggestion will be appreciate...

Array.ConvertAll(strArr ,p=>Convert.ToInt32(p));
Hope this will Work
JayantaChatterjee 12-Apr-13 11:39am
Its look problem solving ans...

can you explain it with my variables names??
JayantaChatterjee 12-Apr-13 11:53am
Faisal Thanks a lottttttttttttttttttttttttt ....
Its works fine... You solve My problems..
ridoy 13-Apr-13 2:29am
Each string has to be converted to an int individually - if your list box contains the values 1, 2, and 3, then your strings will be "1", "2" and "3", which are no integers. They are the string representation of integers instead.
To convert them, you need to convert each element to an integer, using the Parse or TryParse methods.
I would do it this way:
List<int> items = new List<int>();
foreach (string s in listBox1.Items)
foreach (string s in listBox2.Items)
If I needed the data as an array after this, I would convert it:
int[] array = items.ToArray();
JayantaChatterjee 12-Apr-13 11:36am
You are right.. My data (1,2 and 3) is string..
But Sir, I don't want to use loops, If I used loops then I will solve it myself..

is there any way to do it without using loops or using any predefined functions??
Faisal(mfrony) 12-Apr-13 11:42am
var propertyModel1 = new[]
You can take it as an example
OriginalGriff 12-Apr-13 11:45am
You *could* use Linq - but that's just hiding the loop, there is still one involved.
You can't just say "these strings are now arrays" - C# will not let you, because it is what is called a "strongly typed" language - it does not (normally) make conversions between datatypes unless you tell it exactly what to do. In the case of strings to integers, that means using Parse or TryParse (or Convert.ToInt if you are not worried about performance or readability, since Convert checks the datatype and then calls int.Parse anyway)
In the case of a list box where you control the data going in and can be sure it is the string version of an int, use Parse. For user inputs, use TryParse.

But you can't just say "these strings are now ints" as C# will just not let you.
JayantaChatterjee 12-Apr-13 11:54am
I added this line as @Faisal said
intArr = Array.ConvertAll(strArr,p=> Convert.ToInt32(p));
Its solve the problems..
Thank you sir for helping me...
Matt T Heffron 12-Apr-13 13:11pm
But you said "without using loops or using any predefined functions??"
Array.ConvertAll() is pretty obviously a predefined function, which just hides the loop as OriginalGriff has stated.
JayantaChatterjee 13-Apr-13 8:18am
#OriginalGriff's comment is right...

but I said, I don't want to use the loops to do that,(Note:Not in the predefined functions).
I know that the manipulating Array's only possible with the loops, without loops its impossible...
You cannot automatically convert an int to a string; the two are totally different types[^]. And, by extension, you cannot automatically convert an array of ints to an array of strings.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900