473,409 Members | 1,979 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,409 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 1861
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
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: 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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.