Cast String to Date Mystery

G

Guest

I'm having a very strange problem I can't seem to figure out and am hoping
maybe somebody has seen it before.

I get an exception "Cast string to date is invalid" with this chunk of code:

' class member
Private m_invDate As Date

' this code is not exact from the program, but essentially does the same
thing. This is were the exception occurs:

Dim str As String = "12/08/05"
invDate = str


Public Property invDate()
Get
Return m_invDate
End Get
Set(ByVal Value)
If Value Is DBNull.Value Then
Value = Nothing
End If
m_invDate = Value
End Set
End Property

The mystery is the code works fine on some computers and the server, but
throws the exception on a few computers. I've also noticed it may have
something to do with being logged in with admin rights(??). However, I got
the computer to work for a day being logged with the admin rights, but the
next day it started throwing the exception again. I have tried so many
different variations to fix this and have gotten nowhere with it. I would
appreciate any tips.

Thanks.
 
S

Stephany Young

The main issue here is that you are allowing 'the system' to have a 'best
effort' at deciding what you mean and it is encountering 'stuff' that it
can't handle.

Basically, it can't really figure what "12/08/05" is supposed to represent.
For all it knows, it could be any of December 8 2005, August 12 2005, August
5 2012 or May 8 2012. In addition it doesn't even know if you intend the
year part to be in th 21st century or not.

I recommend that you amend your code so that the conversions are explicit
and unambiguous.

The 'difference' on some computers will not have anything to do with 'admin
rights'. If anything it will be to do with differences in the Regional
settings which dictates how the system will go about attempting to convert
the string to a date.

Never assume, because you can tell what something means by 'eyeballing' it,
that a computer can without being instructed exactly how to work it out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top