Invalid cast: date array into object array

B

Brian P

I am getting an invalid cast exception when I try
to take an ArrayList with datetime values and use
the ToArray method to create an object array.

I need to use an object array becase I'm working
with various datatypes and need the code to
be as generic as possible.

Most of to code involves dynamically determining
the datatype, which all works.

The code that fails is where I already know the
type is date time:

object[] objArray = (object[]) list.ToArray(typeof(DateTime));


How can I get an array of date time objects into an object array??

Thanks for any help,
Brian
 
G

Guest

The problem isn’t that you are trying to convert it to an object array, but
that you are never successfully converting it to a DateTime array in the
first place.

For your ToArray, instead of:

list.ToArray(typeof(DateTime));

Try:

list.ToArray(typeof(DateTime[]));

Once you’ve got the correct type of array being generated (or an array
period for that matter, then you can freely convert the resulting array to
any compatible sort, leaving you with:

object[] objArray = (object[]) list.ToArray(typeof(DateTime[]));

Brendan
 
C

cecil

Brian,
Try:
ArrayList al = new ArrayList();
al.Add(DateTime.Now);
al.Add(DateTime.Today);
object[] o = (object[])al.ToArray(typeof(object));
DateTime dt = (DateTime)o[0]; ^^^^^^^
MessageBox.Show(dt.ToLongDateString());

compiles and runs no problem.

Cecil Howell
 
B

Brian P

Hello (e-mail address removed),

This seems to work. I'm still not quite sure why typeof(DateTime) doesn't
work, but I'm not gonna loose any sleep over it.

Thanks!

Brian
 
G

Guest

I’m an idiot, and my response proves it, ignore what I said about changing
typeof(DateTime) to typeof(DateTime[]).

The other poster is correct in their changing it to typeof(object).

<kicking self>


Brendan Grant said:
The problem isn’t that you are trying to convert it to an object array, but
that you are never successfully converting it to a DateTime array in the
first place.

For your ToArray, instead of:

list.ToArray(typeof(DateTime));

Try:

list.ToArray(typeof(DateTime[]));

Once you’ve got the correct type of array being generated (or an array
period for that matter, then you can freely convert the resulting array to
any compatible sort, leaving you with:

object[] objArray = (object[]) list.ToArray(typeof(DateTime[]));

Brendan


Brian P said:
I am getting an invalid cast exception when I try
to take an ArrayList with datetime values and use
the ToArray method to create an object array.

I need to use an object array becase I'm working
with various datatypes and need the code to
be as generic as possible.

Most of to code involves dynamically determining
the datatype, which all works.

The code that fails is where I already know the
type is date time:

object[] objArray = (object[]) list.ToArray(typeof(DateTime));


How can I get an array of date time objects into an object array??

Thanks for any help,
Brian
 
G

Guest

typeof(DateTime) doesn't work because you have declared your array to be of
type object and you are converting your ArrayList to a DateTime[] Array

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk


Brian P said:
Hello (e-mail address removed),

This seems to work. I'm still not quite sure why typeof(DateTime) doesn't
work, but I'm not gonna loose any sleep over it.

Thanks!

Brian
Brian,
Try:
ArrayList al = new ArrayList();
al.Add(DateTime.Now);
al.Add(DateTime.Today);
object[] o = (object[])al.ToArray(typeof(object));
DateTime dt = (DateTime)o[0]; ^^^^^^^
MessageBox.Show(dt.ToLongDateString());
compiles and runs no problem.

Cecil Howell
 
J

Jon Skeet [C# MVP]

billr said:
typeof(DateTime) doesn't work because you have declared your array to be of
type object and you are converting your ArrayList to a DateTime[] Array

Note that this would be fine if DateTime were a reference type, but
it's a value type.
 
G

Guest

Doh! I always forget to include the actual point of my post!

thanks for picking up on that Jon :blush:))
--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk


Jon Skeet said:
billr said:
typeof(DateTime) doesn't work because you have declared your array to be of
type object and you are converting your ArrayList to a DateTime[] Array

Note that this would be fine if DateTime were a reference type, but
it's a value type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top