Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# convert arrays
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 -:
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 12-Apr-13 5:37am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Array.ConvertAll(strArr ,p=>Convert.ToInt32(p));
Hope this will Work
  Permalink  
Comments
Jayanta Chatterjee at 12-Apr-13 11:39am
   
Its look problem solving ans...
 
can you explain it with my variables names??
Jayanta Chatterjee at 12-Apr-13 11:53am
   
Faisal Thanks a lottttttttttttttttttttttttt ....
Its works fine... You solve My problems..
thanksssssss..
ridoy at 13-Apr-13 2:29am
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
            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:
            int[] array = items.ToArray();
  Permalink  
v2
Comments
Jayanta Chatterjee at 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) at 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 at 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.
Jayanta Chatterjee at 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 at 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.
Jayanta Chatterjee at 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...

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

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 12 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100