Click here to Skip to main content
Rate this: bad
good
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
dj4400196
Edited 16-Sep-10 7:13am
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 at 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 at 4-Sep-14 6:57am
   
this solution are not working so erase it,and give right solution
Member 11058486 at 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 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 at 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 at 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

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

  Print Answers RSS
0 OriginalGriff 400
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 16 Sep 2010
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