473,549 Members | 2,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ctype(Stinrg,Da te) 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 2810
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 "Refactorin g to
Patterns" from Addison Wesley.

Hope this helps
Jay

<jb> wrote in message news:41******** *************** @news.easynet.c o.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******** *****@tk2msftng p13.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 "Refactorin g to Patterns" from Addison Wesley.

Hope this helps
Jay

<jb> wrote in message news:41******** *************** @news.easynet.c o.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.T oString)
testdates(New DateTime(2004, 1, 3).ToString)
testdates("blab lalba")
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:0 z")
It validates OK for me, but it shouldnt.

--
Jonathan Bailey.

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:Oa******** *****@tk2msftng p13.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.T oString)
testdates(New DateTime(2004, 1, 3).ToString)
testdates("blab lalba")
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******** ******@TK2MSFTN GP10.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******** *****@tk2msftng p13.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 "Refactorin g

to
Patterns" from Addison Wesley.

Hope this helps
Jay

<jb> wrote in message
news:41******** *************** @news.easynet.c o.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
2300
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 similar name to the table. Rather than making a long Select Case that could become obsolete if lookup tables are added and the source table of the...
0
927
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
10589
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 cbx_ccjDateNone.Checked = True then dateCCJApprovedDate = ctype("", DateTime) else dateCCJApprovedDate =...
2
11082
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 type 'Date' is not valid. I use this command change string to Date dim year as date dim strYear as string => year = ctype(stryear,Date).ADDYEAR(2)...
6
9061
by: John A Grandy | last post by:
what's the preferred method in vb.net to convert a String to a Date .... ?
11
1553
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 'Mon' then result should be 2. Thank you, Simon
3
2200
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 Page.FindControl in the following code: ******************************************
4
13105
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 format into Date format and I'm not sure how. I tried this and I thought it would work, but I get the
5
1965
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
1402
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 records to be returned that have dates such as 5/14/2008 and when the start date is selected as 5/14/2008 the query doesn't include it unless I...
0
7477
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...
0
7750
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. ...
0
7838
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...
0
6084
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...
1
5396
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5118
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...
0
3524
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...
0
3503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1084
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.