473,396 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

downcasting from a System.Array to an array of a specific type

I'm trying to do this-

....
void method1(System.Array arr)
{
// happen to know all elements in arr are int's

// this line throws an InvalidCastException
int[] intArr = (int[]) arr;
}

yes, it's asking a bit much to have it work with that simple of syntax, but
is there any way to downcast from a System.Array to a <specific type>[]
(e.g., int[]), as an alternative to creating a new specific type array, and
copying from the System.Array?
Nov 17 '05 #1
4 2171
Do you really want to cast the array itself? Why not simply cast the
elements as you retrieve them.

Pete

"Joe Doyle" <Jo******@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
I'm trying to do this-

...
void method1(System.Array arr)
{
// happen to know all elements in arr are int's

// this line throws an InvalidCastException
int[] intArr = (int[]) arr;
}

yes, it's asking a bit much to have it work with that simple of syntax,
but
is there any way to downcast from a System.Array to a <specific type>[]
(e.g., int[]), as an alternative to creating a new specific type array,
and
copying from the System.Array?

Nov 17 '05 #2
Joe Doyle wrote:
I'm trying to do this-

...
void method1(System.Array arr)
{
// happen to know all elements in arr are int's

// this line throws an InvalidCastException
int[] intArr = (int[]) arr;
}

yes, it's asking a bit much to have it work with that simple of syntax, but
is there any way to downcast from a System.Array to a <specific type>[]
(e.g., int[]), as an alternative to creating a new specific type array, and
copying from the System.Array?


You should be able to see why this isn't possible. The fact that you
'happen to know all elements ... are ints' is what allows you to safely
do an Array.Copy into an int[]. If it *were* possible to somehow do
this cast, then picture this:

object[] arr = new object[] { 1,2,3 };
System.Array a = arr; // to hide its type from the compiler
int[] intarr;

// suppose this line worked:
intarr = (int[]) a;

arr[2] = new Hashtable();

// what is int[2] now??

Casting only makes sense when the thing actually *is* of the type you
are trying to cast to. Just because an array only contains ints, that
doesn't make it an int[].

--
Larry Lard
Replies to group please

Nov 17 '05 #3

"Joe Doyle" <Jo******@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
void method1(System.Array arr)
{
// happen to know all elements in arr are int's

// this line throws an InvalidCastException
int[] intArr = (int[]) arr;
}

yes, it's asking a bit much to have it work with that simple of syntax, but
is there any way to downcast from a System.Array to a <specific type>[]
(e.g., int[]), as an alternative to creating a new specific type array, and
copying from the System.Array?


Where do you get the System.Array in the first place?
Do you really have an int[] or
do you have an object[] full of boxed ints?

Bill
Nov 17 '05 #4
Joe Doyle wrote:
I'm trying to do this-

...
void method1(System.Array arr)
{
// happen to know all elements in arr are int's

// this line throws an InvalidCastException
int[] intArr = (int[]) arr;
}


You will get an InvalidCastException if arr is *not* an int[]. Did you
check this?

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Stuart Golodetz | last post by:
Hi, I've got a minor casting issue which I need to check about (marked // <--). I was trying to do a static_cast on it (which didn't work, though I'm not sure why this should be the case?) I...
9
by: Simon Elliott | last post by:
If I have a base class and several possible derived classes, and I have a pointer to the base class, what's the best way of determining whether the pointer is pointing to a base class or to a...
2
by: Michael | last post by:
Ok Guys, on the same thread I'm writing a Game engine, which performs collision detection. I want to sepate the collision boundings libaray from the main game engine and load it as a library....
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
6
by: Fernando Rodríguez | last post by:
Hi, I have a class called UrlFetcher and keep several instances inside an ArrayList. This class has a fecth() method. When I iterate through the list calling the fetch() method, I have to...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
5
by: Michael Primeaux | last post by:
I have a simple .NET 2.0 web service created with VS.NET 2005 with a single web method with the following signature: void HelloWorld(Guid parameter1); When calling this method I receive the...
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
5
by: Ryan van Roode | last post by:
Hello, An API function gives me an object of class 'Blah'. I have subclassed 'Blah' and added a few methods: class EnhancedBlah extends Blah { function enhancement1() {} function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.