Invalid Cast

San

Newcomer
Joined
Jun 19, 2003
Messages
5
Location
Bangalore, India
I am trying with the below code which is part of a function but I get and error message saying Run-time exception thrown : System.InvalidCastException - Cast from string "21/06/2003" to type 'Date' is not valid
code is
dim dtRentalMonth as date
dim m_strMonYearMonth as string

' Value stored in m_strMonYearMonth is "200306"

dtRentalMonth = Format(m_strMonYearMonth, "0000-00-21")

I have tried using cdate and ctype functions but it is of no use
this willwork fine in vb6.0. in vb6.0 i get the result as 21/06/2003.
Any help on this will be appreciated.

Regards,
Santhosh
 
San , this depends on your system's settings. Cdate would work fine for you but it seems that your system's date settings are

MM/DD/YYYY not DD/MM/YYYY so set your string to 06/21/2003 and use cdate and everything will work fine.
 
PlausiblyDamp is correct, you need to use Date.Parse() to turn the
formatted string into a Date; you cannot just set a string to
date because they are different types (apparently VB6 converts it
automatically).

There is an example in MSDN that shows how to convert dates that
are formatted for different cultures:
[mshelp]ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemDateTimeClassParseTopic.htm[/mshelp]
 
Back
Top