473,806 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ty peof(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 1897
Brian,
Try:
ArrayList al = new ArrayList();
al.Add(DateTime .Now);
al.Add(DateTime .Today);
object[] o = (object[])al.ToArray(typ eof(object));
DateTime dt = (DateTime)o[0]; ^^^^^^^
MessageBox.Show (dt.ToLongDateS tring());

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(ty peof(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(ty peof(DateTime)) ;

Try:

list.ToArray(ty peof(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(ty peof(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(ty peof(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(typ eof(object));
DateTime dt = (DateTime)o[0]; ^^^^^^^
MessageBox.Show (dt.ToLongDateS tring());
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(ty peof(DateTime)) ;

Try:

list.ToArray(ty peof(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(ty peof(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(ty peof(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(typ eof(object));
DateTime dt = (DateTime)o[0]; ^^^^^^^
MessageBox.Show (dt.ToLongDateS tring());
compiles and runs no problem.

Cecil Howell


Aug 17 '05 #6
billr <bi***@discussi ons.microsoft.c om> 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.co m>
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***@discussi ons.microsoft.c om> 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.co m>
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
452
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
1687
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. 3. Same problem on two different computers running Win 2003.
7
2326
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 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.
5
3447
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
2
2239
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 cast exception error: Compiler Error Message: BC30311: Value of type 'Date' cannot be converted to 'Integer'. The real problem here is that the type in the database and the stored prcedure aren't integers at all rather they are datetime...
3
2295
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 DataSet ' Create connection Dim cn As New OleDbConnection With cn .connectionstring =
1
4912
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 specifiying my code: The Code used in Visual Studio: Function GetRank(ByVal ID As Integer, ByVal Comp As String, ByVal Sec As String, ByVal iDate As Date) As String 'Dim Ret As Integer
7
1870
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 crashing my program. it complains about an invalid cast. i don't understand why though. I tried putting a IsDBNull around
2
4926
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 System.Text; using System.Windows.Forms;
0
9597
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10620
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10372
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6877
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5682
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.