473,385 Members | 1,610 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,385 software developers and data experts.

Ctype(Stinrg,Date) works with non-numerics

I am trying to validate a textfield to ensure it holds a time. I use a
try/catch block to see if I can convert to a time & if it works then I
blindly 'assume' it was OK.

This single line

Dim y As Date = CType("00:0z", Date)

gives me a time of 1:00 am, whereas
Dim z As Date = Date.Parse("00:0z")

gives me the same time but with today's date
Is this a bug or a feature of the framework ? Does anyone have a better idea
?

--
Jonathan Bailey.
Nov 21 '05 #1
7 2797
Jonathan,
Is this a bug or a feature of the framework ? I don't consider it a bug nor a feature.

CType & CDate are VB.NET functions that have defined functionality.

DateTime.Parse is a BCL (Base class library) method that has defined
functionality.

Although a number of the VB.NET functions are implemented in terms of the
BCL, some have their own specific behaviors, normally I find the VB.NET
functions will try harder, in that they are more tolerant of invalid values
(indexes out of range & such).
Does anyone have a better idea? I wouldn't mix & match DateTime.Parse & CDate in the same program if I
wanted consistent results.

Just like I don't mix String methods with VB.Strings methods, as the String
methods are 0 based indexing, while VB.Strings are 1 based indexing.

Both are examples of "Oddball Solution smell" which means "When a problem is
solved one way throughout a system and the same problem is solved another
way in the same system, one of the solutions is hte oddball or inconsistent
solution", Oddball here means using DateTime.Parse & CDate, not that one of
them returns a different result. The different result is just an extra
factor in the puzzle...

Oddball Solution Smell is defined in Joshua Kerievsky's book "Refactoring to
Patterns" from Addison Wesley.

Hope this helps
Jay

<jb> wrote in message news:41***********************@news.easynet.co.uk. ..I am trying to validate a textfield to ensure it holds a time. I use a
try/catch block to see if I can convert to a time & if it works then I
blindly 'assume' it was OK.

This single line

Dim y As Date = CType("00:0z", Date)

gives me a time of 1:00 am, whereas
Dim z As Date = Date.Parse("00:0z")

gives me the same time but with today's date
Is this a bug or a feature of the framework ? Does anyone have a better
idea
?

--
Jonathan Bailey.

Nov 21 '05 #2
I found that timespan.parse seems to validate better. I have to eliminate
any response with a '.' as that would allow a number of days. Any other
suggestions to validate just hh:mm:ss ?

--
Jonathan Bailey.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uA*************@tk2msftngp13.phx.gbl...
Jonathan,
Is this a bug or a feature of the framework ? I don't consider it a bug nor a feature.

CType & CDate are VB.NET functions that have defined functionality.

DateTime.Parse is a BCL (Base class library) method that has defined
functionality.

Although a number of the VB.NET functions are implemented in terms of the
BCL, some have their own specific behaviors, normally I find the VB.NET
functions will try harder, in that they are more tolerant of invalid

values (indexes out of range & such).
Does anyone have a better idea? I wouldn't mix & match DateTime.Parse & CDate in the same program if I
wanted consistent results.

Just like I don't mix String methods with VB.Strings methods, as the

String methods are 0 based indexing, while VB.Strings are 1 based indexing.

Both are examples of "Oddball Solution smell" which means "When a problem is solved one way throughout a system and the same problem is solved another
way in the same system, one of the solutions is hte oddball or inconsistent solution", Oddball here means using DateTime.Parse & CDate, not that one of them returns a different result. The different result is just an extra
factor in the puzzle...

Oddball Solution Smell is defined in Joshua Kerievsky's book "Refactoring to Patterns" from Addison Wesley.

Hope this helps
Jay

<jb> wrote in message news:41***********************@news.easynet.co.uk. ..
I am trying to validate a textfield to ensure it holds a time. I use a
try/catch block to see if I can convert to a time & if it works then I
blindly 'assume' it was OK.

This single line

Dim y As Date = CType("00:0z", Date)

gives me a time of 1:00 am, whereas
Dim z As Date = Date.Parse("00:0z")

gives me the same time but with today's date
Is this a bug or a feature of the framework ? Does anyone have a better
idea
?

--
Jonathan Bailey.


Nov 21 '05 #3
Jb,

This is the normal one, which I made in a complete sample for you to show
it.
(the important part for you is only "IsDate"

\\\
Public Module testDates
Public Sub Main()
testdates(Now.ToString)
testdates(New DateTime(2004, 1, 3).ToString)
testdates("blablalba")
End Sub
Private Sub testdates(ByVal value As String)
If IsDate(value) Then
MessageBox.Show(value & "=I am a correct date")
Else
MessageBox.Show(value & "=I am not a date")
End If
End Sub
End Module
///

I hope this helps?

Cor
Nov 21 '05 #4
Try this one:
testdates("00:0z")
It validates OK for me, but it shouldnt.

--
Jonathan Bailey.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:Oa*************@tk2msftngp13.phx.gbl...
Jb,

This is the normal one, which I made in a complete sample for you to show
it.
(the important part for you is only "IsDate"

\\\
Public Module testDates
Public Sub Main()
testdates(Now.ToString)
testdates(New DateTime(2004, 1, 3).ToString)
testdates("blablalba")
End Sub
Private Sub testdates(ByVal value As String)
If IsDate(value) Then
MessageBox.Show(value & "=I am a correct date")
Else
MessageBox.Show(value & "=I am not a date")
End If
End Sub
End Module
///

I hope this helps?

Cor

Nov 21 '05 #5
JB,

Look at this page, (although there is written a Capital Z Zulu time). So
when this is as strict as written here, it is in my opinion a bug.

http://www.grc.nasa.gov/WWW/MAEL/ag/zulu.htm

However I do not know that, I only looked at this page and one other page
where is stated the same.

Cor
Nov 21 '05 #6
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OI**************@TK2MSFTNGP10.phx.gbl...
JB,

Look at this page, (although there is written a Capital Z Zulu time). So
when this is as strict as written here, it is in my opinion a bug.

http://www.grc.nasa.gov/WWW/MAEL/ag/zulu.htm

However I do not know that, I only looked at this page and one other page
where is stated the same.

Cor


Thanks - We have a system which includes what is known as the 'Disney
date' - this was for those years when a leap year occurred but so as to
allow the users to refer to 31th December as 365 the 29th Febuary was turned
into 366, so maybe a z in the time isnt quite so funny as I thought.

--
Jonathan Bailey.
Nov 21 '05 #7
Jonathan,
I would primarily use TimeSpan.Parse when I needed to parse time spans (a
duration).

I would primarily use either DateTime.Parse or CDate then I needed to parse
dates, times (time of day), or dates w/times. However I would make every
effort to use only DateTime.Parse or CDate within a single solution.

Hope this helps
Jay

<jb> wrote in message news:41**********************@news.easynet.co.uk.. .
I found that timespan.parse seems to validate better. I have to eliminate
any response with a '.' as that would allow a number of days. Any other
suggestions to validate just hh:mm:ss ?

--
Jonathan Bailey.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uA*************@tk2msftngp13.phx.gbl...
Jonathan,
> Is this a bug or a feature of the framework ?

I don't consider it a bug nor a feature.

CType & CDate are VB.NET functions that have defined functionality.

DateTime.Parse is a BCL (Base class library) method that has defined
functionality.

Although a number of the VB.NET functions are implemented in terms of the
BCL, some have their own specific behaviors, normally I find the VB.NET
functions will try harder, in that they are more tolerant of invalid

values
(indexes out of range & such).
> Does anyone have a better idea?

I wouldn't mix & match DateTime.Parse & CDate in the same program if I
wanted consistent results.

Just like I don't mix String methods with VB.Strings methods, as the

String
methods are 0 based indexing, while VB.Strings are 1 based indexing.

Both are examples of "Oddball Solution smell" which means "When a problem

is
solved one way throughout a system and the same problem is solved another
way in the same system, one of the solutions is hte oddball or

inconsistent
solution", Oddball here means using DateTime.Parse & CDate, not that one

of
them returns a different result. The different result is just an extra
factor in the puzzle...

Oddball Solution Smell is defined in Joshua Kerievsky's book "Refactoring

to
Patterns" from Addison Wesley.

Hope this helps
Jay

<jb> wrote in message
news:41***********************@news.easynet.co.uk. ..
>I am trying to validate a textfield to ensure it holds a time. I use a
> try/catch block to see if I can convert to a time & if it works then I
> blindly 'assume' it was OK.
>
> This single line
>
> Dim y As Date = CType("00:0z", Date)
>
> gives me a time of 1:00 am, whereas
> Dim z As Date = Date.Parse("00:0z")
>
> gives me the same time but with today's date
>
>
> Is this a bug or a feature of the framework ? Does anyone have a better
> idea
> ?
>
> --
> Jonathan Bailey.
>
>



Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Douglas Buchanan | last post by:
I am using the following code instead of a very lengthly select case statement. (I have a lot of lookup tables in a settings form that are selected from a ListBox. The data adapters are given a...
0
by: Child | last post by:
trying to edit a date in a datagrid. i need this to be a date: birthday = ctype(e.item.cells(6).controls(0), textbox).text how do i do that? -- BethF, Anchorage, AK It's YOUR God.
5
by: darrel | last post by:
I have the following right now to enter a date into SQL getting the data from some pull down menus: ------------------------------------------------- dim dateCCJApprovedDate as DateTime if...
2
by: Ryanfai | last post by:
I use the vb.net make the program that save the date and any Information field. when I input the date in textbox then I save to database occurs error that is "Cast from string "20/08/2003" to...
6
by: John A Grandy | last post by:
what's the preferred method in vb.net to convert a String to a Date .... ?
11
by: simon | last post by:
I have the name of a day like for example 'Mon' Is there some function which returns the number of a weekDay if you have the day name or I should write the select case statements? If the day is...
3
by: tshad | last post by:
I have a .ascx file that I converted to a class. But I am getting the following error: error BC30469: Reference to a non-shared member requires an object reference. The error is for the...
4
by: Phillip Vong | last post by:
VS2005 in VB.NET I have 2 simple textboxes (Textbox1, Textbox2) and they both have dates. Textbox1=12/31/2006 Textbox2=12/15/2006 I need to convert these two textboxes that are in String...
5
by: c_shah | last post by:
I have a string in this format 100106 what is the most efficient way to convert it to a date like 10/01/2006 thanks.
2
by: Tony K | last post by:
I have a form that involves a datagrid view to be filled after a start and end date are selected from 2 DateTimePicker fields using the Short Format. The query works without error but... I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.