472,789 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 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 1827
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: roni | last post by:
i have array of objects that's are strings. i need to convert it to array of string before sending to method. is there a way to convert ? (short way..)
0
by: Alan Z. Scharf | last post by:
Win Server 2003 VS.Net 2003 --------------- 1. I'm having the same problem below on all six of my pages with a datagrid item. 2. These pages all worked fine for months until problem started....
7
by: Brian P | last post by:
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...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
2
by: adams114 | last post by:
I am having a strange problem with invalid type casts. I am trying to update a MS SQL Database with a stored procedure. When I setup the parameters collection for the command object I get a invalid...
3
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
1
by: Hifni Shahzard | last post by:
Hi, I got a stored procedure, where it returns a value. But if I execute it. It gives an error as "Invalid cast from System.Int32 to System.Byte.". To make clear how do I execute this, below I'm...
7
by: chance | last post by:
Getting and error on this first line of code: //inspector phone theField = reader.GetString(20); builder.MoveToBookmark("inspector_phone"); builder.Write(theField); the first line is...
2
by: eruss21 | last post by:
Here's my code. Don't know what's going on. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.