471,344 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Invalid cast: date array into object array



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
Aug 17 '05 #1
7 1759
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

Brian P wrote:
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


Aug 17 '05 #2
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" wrote:


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

Aug 17 '05 #3
Hello ce***@ceciltech.com,

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

Aug 17 '05 #4
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" wrote:
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" wrote:


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

Aug 17 '05 #5
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" wrote:
Hello ce***@ceciltech.com,

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


Aug 17 '05 #6
billr <bi***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Aug 17 '05 #7
Doh! I always forget to include the actual point of my post!

thanks for picking up on that Jon :o))
--
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 [C# MVP]" wrote:
billr <bi***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Aug 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Alan Z. Scharf | last post: by
7 posts views Thread by Brian P | last post: by
5 posts views Thread by Nick Flandry | last post: by
2 posts views Thread by adams114 | last post: by
3 posts views Thread by John Howard | last post: by
1 post views Thread by Hifni Shahzard | last post: by

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.