Click here to Skip to main content
12,069,857 members (68,579 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hi,
i have a simple question:
can i explicitly cast an object array to an int array?

probably not because the object array can contain floats or longs etc..
D'Oh! | :doh:
thanks
dj4400
Posted 16-Sep-10 7:06am
dj4400199
Edited 16-Sep-10 7:13am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Look at ConvertAll. That allows you to specify a function to perform the conversion. Since you indicated the array may contain non-integer data types (such as float), you will need to perform an explicit cast to truncate the values.
  Permalink  
Comments
aspdotnetdev 16-Sep-10 15:37pm
   
Also, this works with the .Net Framework 2.0, so it's more flexible than the generic Cast method.
samqty 16-Sep-10 15:48pm
   
alright!!! must say this is a better approach, you learn something new everyday. Thanks!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You can't convert an integer array into an object array. But you can do the following:

object[] a = new object[] { (int)1, (int)2 }
or
object b = (object)(new int[] { 1, 2 })

-----------------------

Sorry, getting turned around by your question not matching your original title.

But ultimately, it follows the inverse of the above rules. You can cast the contents of an object array to an integer. You can't just do it directly with the reference of the object array. You have to iterate over each of the objects and cast them one at a time.
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

if you are using .net 3.5 this might help you
object[] objarr = new object[] {1,2,3 };
 int[] arr= objarr.Cast<int>().ToArray();

its a neat method included with .net 3.5 that allows you to cast the entire collection.

Hope that helped
good luck
  Permalink  
Comments
aspdotnetdev 16-Sep-10 15:28pm
   
This will not work, as the OP indicated the array may contain other types that can't be directly assigned to an int, such as a float, which would require an explicity cast. Try using 5.5 instead of 1 in your array.
Member 11058486 4-Sep-14 6:57am
   
this solution are not working so erase it,and give right solution
Member 11058486 4-Sep-14 7:01am
   
http://www.codeproject.com/script/Forums/Messages.aspx?fmid=11058486
on this page when i page up and down ,your theam are spreads so coorect it
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

var objectArray = new object[] {
                    1, 2.2, "abc"
                };
var intArray = new int[objectArray.Length];
 
for (int i = 0; i < objectArray.Length; i++)
{
    try
    {
        intArray[i] = Convert.ToInt32(objectArray[i]);
    }
    catch
    {
        intArray[i] = 0;
    }
 
}

This "explicitely" converts the objects to integer array...
  Permalink  
Comments
CHill60 23-Dec-15 4:16am
   
The question was asked and answered over 5 years ago!
However as this solution adequately handles strings I won't downvote.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160208.1 | Last Updated 23 Dec 2015
Copyright © CodeProject, 1999-2016
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