Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
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 -:
C#
using System;
using System.Windows.Forms;

namespace JustForFun
{
    public partial class ArrayStringToInt : Form
    {
        public ArrayStringToInt()
        {
            InitializeComponent();
        }

        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.Sort(intArr);
            System.Array.Copy(intArr, strArr, intArr.Length);
            listBox3.Items.AddRange(strArr);
        }
    }
}


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...
Posted

Array.ConvertAll(strArr ,p=>Convert.ToInt32(p));
Hope this will Work
 
Share this answer
 
Comments
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..
thanksssssss..
ridoy 13-Apr-13 2:29am    
+5
No.
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:
C#
List<int> items = new List<int>();
foreach (string s in listBox1.Items)
    {
    items.Add(int.Parse(s));
    }
foreach (string s in listBox2.Items)
    {
    items.Add(int.Parse(s));
    }
If I needed the data as an array after this, I would convert it:
C#
int[] array = items.ToArray();
 
Share this answer
 
v2
Comments
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[]
{
"1","2","3"
};
Array.ConvertAll(propertyModel1,p=>Convert.ToInt32(p));
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.
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.
 
Share this answer
 

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