C
Cor Ligthert
Hello everybody,
Jay and Herfried are telling me every time when I use CDate that using the
datetime.parseexact is always the best way to do String to datetime
conversions. They don't tell why only that I have to listen to them because
they know it better.
They told also that in a business situation it is better to use
datetime.parseexact for changing cultures and not to use the globalization
setting. I did not give them this sample, only told the situation how that
in my opinion has to be done. Jay gave the business sample by the way in
another thread, where he told that using datetime.parseexact was the way to
go for that. However did in that thread as well not tell how and why it was
better, only that he, Herfried and a C# MVP were telling that and therefore
it was better.
I have made a little code example from it, to make it more showable, can
someone tell me how it can be done better using datetime.parseexact as Jay
and Herfried constantly are telling, and why that is better to use in this
kind of situations where the documents can come from all over the world.
Public Class test
Shared Sub main()
Dim d As New document
Dim o As New order
'The next is the setting from an XML file or whatever document
d.culture = "nl-NL"
d.deliverydate = "25 aug 2004"
d.orderdate = "10 juli 2004"
d.price = "? 111,10"
d.quantity = "10.000"
Dim thisErrors As Errors = ConvertDocumentToOrder(d, o)
'Do the debugprinting yourself
'This is another document
d.culture = "en-US"
d.deliverydate = "aug 25 2004"
d.orderdate = "july 10 2004"
d.price = "$ 111.10"
d.quantity = "10,000"
thisErrors = ConvertDocumentToOrder(d, o)
'Do the debugprinting yourself
End Sub
Public Shared Function ConvertDocumentToOrder(ByVal d As document, _
ByVal o As order) As Errors
Dim myError As New Errors
Try
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo(d.culture)
o.orderdate = CDate(d.orderdate)
o.deliverydate = CDate(d.deliverydate)
o.price = CDec(d.price)
o.quantity = CInt(d.quantity)
o.culture = d.culture
Return myError
Catch ex As Exception 'However whatever error set
'evaluate(myerror, ex)
Return myError
Finally
Threading.Thread.CurrentThread.CurrentCulture = _
Globalization.CultureInfo.InstalledUICulture
End Try
End Function
End Class
Public Class document
Public orderdate As String
Public deliverydate As String
Public quantity As String
Public price As String
Public culture As String
End Class
Public Class order 'Normally with properties where validation is used
Public orderdate As DateTime
Public deliverydate As DateTime
Public quantity As Integer
Public price As Decimal
Public culture As String
End Class
Public Class Errors
Public whatever As String
Public Sub New()
whatever = ""
End Sub
End Class
With what I don't say that there can be probably situations where
datetime.parseexact are simpler to use, however that in my opinion when you
know exactly the format of the string(s), that will be used in the document.
Jay and Herfried are telling me every time when I use CDate that using the
datetime.parseexact is always the best way to do String to datetime
conversions. They don't tell why only that I have to listen to them because
they know it better.
They told also that in a business situation it is better to use
datetime.parseexact for changing cultures and not to use the globalization
setting. I did not give them this sample, only told the situation how that
in my opinion has to be done. Jay gave the business sample by the way in
another thread, where he told that using datetime.parseexact was the way to
go for that. However did in that thread as well not tell how and why it was
better, only that he, Herfried and a C# MVP were telling that and therefore
it was better.
I have made a little code example from it, to make it more showable, can
someone tell me how it can be done better using datetime.parseexact as Jay
and Herfried constantly are telling, and why that is better to use in this
kind of situations where the documents can come from all over the world.
Public Class test
Shared Sub main()
Dim d As New document
Dim o As New order
'The next is the setting from an XML file or whatever document
d.culture = "nl-NL"
d.deliverydate = "25 aug 2004"
d.orderdate = "10 juli 2004"
d.price = "? 111,10"
d.quantity = "10.000"
Dim thisErrors As Errors = ConvertDocumentToOrder(d, o)
'Do the debugprinting yourself
'This is another document
d.culture = "en-US"
d.deliverydate = "aug 25 2004"
d.orderdate = "july 10 2004"
d.price = "$ 111.10"
d.quantity = "10,000"
thisErrors = ConvertDocumentToOrder(d, o)
'Do the debugprinting yourself
End Sub
Public Shared Function ConvertDocumentToOrder(ByVal d As document, _
ByVal o As order) As Errors
Dim myError As New Errors
Try
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo(d.culture)
o.orderdate = CDate(d.orderdate)
o.deliverydate = CDate(d.deliverydate)
o.price = CDec(d.price)
o.quantity = CInt(d.quantity)
o.culture = d.culture
Return myError
Catch ex As Exception 'However whatever error set
'evaluate(myerror, ex)
Return myError
Finally
Threading.Thread.CurrentThread.CurrentCulture = _
Globalization.CultureInfo.InstalledUICulture
End Try
End Function
End Class
Public Class document
Public orderdate As String
Public deliverydate As String
Public quantity As String
Public price As String
Public culture As String
End Class
Public Class order 'Normally with properties where validation is used
Public orderdate As DateTime
Public deliverydate As DateTime
Public quantity As Integer
Public price As Decimal
Public culture As String
End Class
Public Class Errors
Public whatever As String
Public Sub New()
whatever = ""
End Sub
End Class
With what I don't say that there can be probably situations where
datetime.parseexact are simpler to use, however that in my opinion when you
know exactly the format of the string(s), that will be used in the document.

arse with the culture is the best bet. I'm not going to set the