473,846 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Globalization versus DateTime.ParseE xact

Hello everybody,

Jay and Herfried are telling me every time when I use CDate that using the
datetime.parsee xact 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.parsee xact 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.parsee xact 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.parsee xact 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 = ConvertDocument ToOrder(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 = ConvertDocument ToOrder(d, o)
'Do the debugprinting yourself
End Sub
Public Shared Function ConvertDocument ToOrder(ByVal d As document, _
ByVal o As order) As Errors
Dim myError As New Errors
Try
Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo(d.cu lture)
o.orderdate = CDate(d.orderda te)
o.deliverydate = CDate(d.deliver ydate)
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(myerr or, ex)
Return myError
Finally
Threading.Threa d.CurrentThread .CurrentCulture = _
Globalization.C ultureInfo.Inst alledUICulture
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.parsee xact 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.
Nov 21 '05 #1
11 7261
"Cor Ligthert" <no************ @planet.nl> schrieb:
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 = ConvertDocument ToOrder(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 = ConvertDocument ToOrder(d, o)
'Do the debugprinting yourself
End Sub
Public Shared Function ConvertDocument ToOrder(ByVal d As document, _
ByVal o As order) As Errors
Dim myError As New Errors
Try
Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo(d.cu lture)
o.orderdate = CDate(d.orderda te)
o.deliverydate = CDate(d.deliver ydate)
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(myerr or, ex)
Return myError
Finally
Threading.Threa d.CurrentThread .CurrentCulture = _
Globalization.C ultureInfo.Inst alledUICulture


Same bug. This will not necessarily reset the culture to the culture used
before.

Instead of using 'CDate' and changing the culture, I would use
'DateTime.Parse ' (= 'Date.Parse') and specify the 'DateTimeInfo':

\\\
.... = Date.Parse(d.Di liveryDate, ci.DateTimeForm at)
///

.... 'ci' is your custom culture info with the custom date/time format set.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #2
>
Instead of using 'CDate' and changing the culture, I would use
'DateTime.Parse ' (= 'Date.Parse') and specify the 'DateTimeInfo':

\\\
... = Date.Parse(d.Di liveryDate, ci.DateTimeForm at)
///

This is this newsgroup almost full of, I asked why, what is the benefit, I
see only more to type words?

And what about all the other culture depending values?

Cor
Nov 21 '05 #3
"Cor Ligthert" <no************ @planet.nl> schrieb:
Instead of using 'CDate' and changing the culture, I would use
'DateTime.Parse ' (= 'Date.Parse') and specify the 'DateTimeInfo':

\\\
... = Date.Parse(d.Di liveryDate, ci.DateTimeForm at)
///

This is this newsgroup almost full of, I asked why, what is the benefit, I
see only more to type words?

And what about all the other culture depending values?


Most 'Parse' methods expect a format information, for example
'Int32.Parse'.. .

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #4
Cor,
Jay and Herfried are telling me every time when I use CDate that using the
datetime.parsee xact is always the best way to do String to datetime Ah! There's the rub, you are talking Oranges; Herfried, Jon & I are talking
Apples! :-)

The Oranges:

Actually in Your Example I would use DateTime.Parse:

Instead of: Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo(d.cu lture)
o.orderdate = CDate(d.orderda te)
o.deliverydate = CDate(d.deliver ydate)
I would use:
Dim culture As New Globalization.C ultureInfo(d.cu lture)

o.orderdate = DateTime.Parse( d.orderdate, culture)
o.deliverydate = DateTime.Parse( d.deliverydate, culture)

Notice that I am simply passing the culture required in the above, rather
then relying on the "global variable" CurrentCulture. By passing the cutlure
as a parameter, I don't need a Try/Finally to restore the "global variable"
CurrentCulture. Also I do not risk changing the "global variable"
CurrentCulture that routines that ConvertDocument ToOrder may call require
set in a specific way. Yes you want it changed for CDate, however you may
not want it changed for CDec or CInt! For example changing the decimal
separator from a comma to a period. Plus you may call one of your own
routines (or team's routines) that requires the CurrentCulture to be a
specific value.

I hope you agree tracking down problems with "global variables" in a program
can be very difficult, especially when they are being set & reset all over
the program, as opposed to being set once & left alone...

All the formatting information needed by DateTime.Parse is contained in the
passed CutltureInfo object, As Herfried showed, DateTime.Parse will notice
that its a CultureInfo & actually use CultureInfo.Dat eTimeFormat property.
Likewise with CDec & CInt, if they need to follow the specific culture,
there is Decimal.Parse & Integer.Parse that accepts a CultureInfo object.
o.price = Decimal.Parse(d .price, culture)
o.quantity =Integer.Parse( d.quantity, culture)
Both Decimal.Parse & Integer.Parse will see the CultureInfo and actually use
CultureInfo.Num berFormat.

CDate, CDec, & CInt use the same CultureInfo object, they just don't allow
you to pass it directly, instead they rely on a global variable... Maybe MS
could simplify it for you & beginner VB.NET developers by allowing you to
specify the format or culture on VB.NET's conversion functions. Rather then
try to explain why you might use DateTime.Parse or DateTime.ParseE xact
instead of the CDate. Would you understand it better if you could do the
following?
Dim ci As New Globalization.C ultureInfo(d.cu lture) o.orderdate = CDate(d.orderda te, ci)
o.deliverydate = CDate(d.deliver ydate, ci)
o.price = CDec(d.price, ci)
o.quantity = CInt(d.quantity , ci)
In other words: CDate, CDec, & CInt do not allow you to specify the
CultureInfo needed, I am suggesting using DateTime.Parse rather then change
a global variable/setting.

Note: In your example, you are not restoring the original value of the
"global variable" CurrentCulture. At the very least save the current
CurrentCulture, then return the CurrentCulture to the one you save.

Dim startingCulture As CultureInfo =
Threading.Threa d.CurrentThread .CurrentCulture Try
Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo(d.cu lture)
o.orderdate = CDate(d.orderda te)
o.deliverydate = CDate(d.deliver ydate)
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(myerr or, ex)
Return myError
Finally
Threading.Threa d.CurrentThread .CurrentCulture = _ startingCulture End Try

Consider what happens if at the top of the Main routine the CurrentCulture
is set to a specific culture:
Shared Sub main() ' we have many different installed cultures on our network
' we need to insure that German - Switzerland is used all the time!
Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo("de-CH")
Dim d As New document
More then likely the above would be an ASP.NET app, and the CurrentCulture
is determined by the login routine, seeing as the ASP.NET app is world wide,
the CurrentCulture will be very different then the InstalledUICult ure!
The Apples:

The other examples include a specific date format, they have nothing to do
with changing the culture!

For example, if I am exchanging text or XML documents between the US, The
Netherlands, Switzerland, Germany & Japan, having the date in a specific
format, such as Year, Month, Day is required otherwise we would get Oct 1
confused with Jan 10. In this case I would use DateTime.ParseE xact to
convert the date string in a specific format in the text document to an
actual DateTime object.

' dates are always in Year, Month, Day format! o.orderdate = DateTime.ParseE xact(d.orderdat e, "yyyy/MM/dd",
nothing)
o.deliverydate = DateTime.ParseE xact(d.delivery date,
"yyyy/MM/dd", nothing)
In the two aforementioned threads I am discussing this last scenario.

I'm really hoping that this is helping!
Jay

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. .. Hello everybody,

Jay and Herfried are telling me every time when I use CDate that using the
datetime.parsee xact 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.parsee xact 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.parsee xact 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.parsee xact 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 = ConvertDocument ToOrder(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 = ConvertDocument ToOrder(d, o)
'Do the debugprinting yourself
End Sub
Public Shared Function ConvertDocument ToOrder(ByVal d As document, _
ByVal o As order) As Errors
Dim myError As New Errors
Try
Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo(d.cu lture)
o.orderdate = CDate(d.orderda te)
o.deliverydate = CDate(d.deliver ydate)
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(myerr or, ex)
Return myError
Finally
Threading.Threa d.CurrentThread .CurrentCulture = _
Globalization.C ultureInfo.Inst alledUICulture
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.parsee xact 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.

Nov 21 '05 #5

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com>
Cor,
Jay and Herfried are telling me every time when I use CDate that using
the datetime.parsee xact is always the best way to do String to datetime Ah! There's the rub, you are talking Oranges; Herfried, Jon & I are
talking Apples! :-)

The Oranges:

Actually in Your Example I would use DateTime.Parse:

Instead of:
Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo(d.cu lture)
o.orderdate = CDate(d.orderda te)
o.deliverydate = CDate(d.deliver ydate)


I would use:
Dim culture As New Globalization.C ultureInfo(d.cu lture)

o.orderdate = DateTime.Parse( d.orderdate, culture)
o.deliverydate = DateTime.Parse( d.deliverydate, culture)


Why?

I am asking that in every message however did not get an answer not from you
and not from Herfried. Microsoft advises to use the Microsoft.Visua lBasic
conversion functions for this. Do you know something that there is wrong in
documentation from Microsoft about this.? Or is there a piece of
documentation I never saw. Until that I relly on this from this page.

=============== =============== =============== ============
http://msdn.microsoft.com/library/de...tinternals.asp
Conversion Functions, CType, DirectCast, and System.Convert
Visual Basic .NET includes data type conversion keywords, many of which are
carried over from Visual Basic 6. But unlike the Visual Basic 6 functions,
these keywords are not function calls but intrinsic language features. The
keywords CBool, CByte, CChar, CShort, CInt, CLng, CSng, CDbl, CDec, CDate,
CObj, and CStr map to Visual Basic Runtime method calls, .NET Framework
class library method calls, or IL type conversion instructions. The exact
method call or IL instructions generated depends on the expression against
which the conversion is being applied. Some conversions are optimized away,
such as CInt(123.45) which is replaced with the integer constant 123 in the
IL. This is an example where using the Visual Basic Runtime results in
better performance than using the System namespace. CInt("123") becomes a
call that leads to calls to System.Double.P arse then System.Math.Rou nd.
CStr(4853) is ultimately handled by System.Int32.To String. Conversions that
are not optimized away eventually lead to methods in the System namespace,
but there is no significant performance benefit when using the System
namespace methods directly. Furthermore, the Visual Basic compiler is able
to perform certain optimizations on conversions using the language keywords
that it does not perform on conversions done through the System namespace.

--------------------------------------------------------------------------------------- Notice that I am simply passing the culture required in the above, rather
then relying on the "global variable" CurrentCulture. By passing the
cutlure as a parameter, I don't need a Try/Finally to restore the "global
variable"
The Try/Finally is not for the global variable setting however to catch
mistypings and throwed errors by the validation from the orderclass wich can
than be catched and processed by the sender of the DocumentToOrder
procedure.
CurrentCulture. Also I do not risk changing the "global variable"
CurrentCulture that routines that ConvertDocument ToOrder may call require
set in a specific way. Yes you want it changed for CDate, however you may
not want it changed for CDec or CInt! For example changing the decimal
separator from a comma to a period. Plus you may call one of your own
Obvious I want to change those, do you not know that they are part of
culture settings and in an ASPNET application would be than very important.
routines (or team's routines) that requires the CurrentCulture to be a
specific value.
What I would do in my orderclass, with the culture however I thougth that
everybody would understand that.
I hope you agree tracking down problems with "global variables" in a
program can be very difficult, especially when they are being set & reset
all over the program, as opposed to being set once & left alone...
You are using them in every line. That is exact why I tell to use a global
setting and when that is build in, use that. .
All the formatting information needed by DateTime.Parse is contained in
the passed CutltureInfo object, As Herfried showed, DateTime.Parse will
notice that its a CultureInfo & actually use CultureInfo.Dat eTimeFormat
property. Likewise with CDec & CInt, if they need to follow the specific
culture, there is Decimal.Parse & Integer.Parse that accepts a CultureInfo
object.
o.price = Decimal.Parse(d .price, culture)
o.quantity =Integer.Parse( d.quantity, culture)

Again you avoid the avised methodes by Microsoft, can you give the
documentation where this is stated by Microsoft as better, it seems to me
code witch has very much change on errors.
Both Decimal.Parse & Integer.Parse will see the CultureInfo and actually
use CultureInfo.Num berFormat.

CDate, CDec, & CInt use the same CultureInfo object, they just don't allow
you to pass it directly, instead they rely on a global variable... Maybe
MS
I am very lucky with that, it is one of the basics from OOP programming to
narrowing everything as much as possible.
could simplify it for you & beginner VB.NET developers by allowing you to
Again I am not a beginner and it is not needed to simplify it for me, I can
everything understand what you and Herfried and Jon are writting. However I
use documentation you know not just empty writting, so show that.

And again do not make the suggestion that I am a beginner. I have pasted 20
replies I think with an answer in the same trend, however I stuffed them
all, I do not go to that low level of arguing.
specify the format or culture on VB.NET's conversion functions. Rather
then try to explain why you might use DateTime.Parse or
DateTime.ParseE xact instead of the CDate. Would you understand it better
if you could do the following?
I showed you the Microsoft documentation, show yours. I think Microsoft did
a good job with that documtation, I do not understand your critique on that.
Dim ci As New Globalization.C ultureInfo(d.cu lture)
o.orderdate = CDate(d.orderda te, ci)
o.deliverydate = CDate(d.deliver ydate, ci)
o.price = CDec(d.price, ci)
o.quantity = CInt(d.quantity , ci)


In other words: CDate, CDec, & CInt do not allow you to specify the
CultureInfo needed, I am suggesting using DateTime.Parse rather then
change a global variable/setting.


Again see my sentence by narrowing the change on errors.

By the way it was all the time DateTime.ParseE xact where you and Herfried
where talking about as the best way to go, Google archives everything you
know. .

Note: In your example, you are not restoring the original value of the
"global variable" CurrentCulture. At the very least save the current
CurrentCulture, then return the CurrentCulture to the one you save.
Dim startingCulture As CultureInfo =
Threading.Threa d.CurrentThread .CurrentCulture
Try
Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo(d.cu lture)
o.orderdate = CDate(d.orderda te)
o.deliverydate = CDate(d.deliver ydate)
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(myerr or, ex)
Return myError
Finally
Threading.Threa d.CurrentThread .CurrentCulture = _ startingCulture
End Try

Consider what happens if at the top of the Main routine the CurrentCulture
is set to a specific culture:
Shared Sub main()

' we have many different installed cultures on our network
' we need to insure that German - Switzerland is used all the time!
Threading.Threa d.CurrentThread .CurrentCulture =


Did you see that Thread.CurrentT hread.CurrentCu lture, dotNet is working with
threads which in themself are processing the steps, step by step so it will
be set in the end of my procedure evertime to the basic culture. Or by
setting it using the standard culture saved before when there is a bug as
Herfried wrote, that is really beginners work.
New Globalization.C ultureInfo("de-CH")

They are in the Net, you do not have to set them.
Dim d As New document


More then likely the above would be an ASP.NET app, and the CurrentCulture
is determined by the login routine, seeing as the ASP.NET app is world
wide, the CurrentCulture will be very different then the
InstalledUICult ure!


My sample is exatly made for ASPNET, it seems you did not read it, it should
work for documents from all over the world not only for Japan, Germanny, the
Netherlands, etc. where a culture setting is.

And in my sample you have nothing to do for it. (With the exception that the
IIS server variables are at the moment not sufficient for it, so we have to
wait on better ones, by instance the clientsertifica tes) and therefore needs
there to be field from the client at the moment where he tells his language
and country (by instance for Canada) what than can be done in my opinon by
an checked to a keys of an Idictionary implementing class which gives than
as value the culture.

I would say, try the sample and add some cultures, maybe than you understand
it better.

Cor
Nov 21 '05 #6
Cor
I'm sorry, I am not discussing this matter with you any further.

I am more then willing to help people who are actually willing to be helped.
You however...

Its really not worth my time!

Good bye!

Jay


"Cor Ligthert" <no************ @planet.nl> wrote in message
news:uM******** ********@TK2MSF TNGP09.phx.gbl. ..

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com>
Cor,
Jay and Herfried are telling me every time when I use CDate that using
the datetime.parsee xact is always the best way to do String to datetime

Ah! There's the rub, you are talking Oranges; Herfried, Jon & I are
talking Apples! :-)

The Oranges:

Actually in Your Example I would use DateTime.Parse:

Instead of:
Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo(d.cu lture)
o.orderdate = CDate(d.orderda te)
o.deliverydate = CDate(d.deliver ydate)


I would use:
Dim culture As New Globalization.C ultureInfo(d.cu lture)

o.orderdate = DateTime.Parse( d.orderdate, culture)
o.deliverydate = DateTime.Parse( d.deliverydate, culture)


Why?

I am asking that in every message however did not get an answer not from
you and not from Herfried. Microsoft advises to use the
Microsoft.Visua lBasic conversion functions for this. Do you know something
that there is wrong in documentation from Microsoft about this.? Or is
there a piece of documentation I never saw. Until that I relly on this
from this page.

=============== =============== =============== ============
http://msdn.microsoft.com/library/de...tinternals.asp
Conversion Functions, CType, DirectCast, and System.Convert
Visual Basic .NET includes data type conversion keywords, many of which
are carried over from Visual Basic 6. But unlike the Visual Basic 6
functions, these keywords are not function calls but intrinsic language
features. The keywords CBool, CByte, CChar, CShort, CInt, CLng, CSng,
CDbl, CDec, CDate, CObj, and CStr map to Visual Basic Runtime method
calls, .NET Framework class library method calls, or IL type conversion
instructions. The exact method call or IL instructions generated depends
on the expression against which the conversion is being applied. Some
conversions are optimized away, such as CInt(123.45) which is replaced
with the integer constant 123 in the IL. This is an example where using
the Visual Basic Runtime results in better performance than using the
System namespace. CInt("123") becomes a call that leads to calls to
System.Double.P arse then System.Math.Rou nd. CStr(4853) is ultimately
handled by System.Int32.To String. Conversions that are not optimized away
eventually lead to methods in the System namespace, but there is no
significant performance benefit when using the System namespace methods
directly. Furthermore, the Visual Basic compiler is able to perform
certain optimizations on conversions using the language keywords that it
does not perform on conversions done through the System namespace.

---------------------------------------------------------------------------------------
Notice that I am simply passing the culture required in the above, rather
then relying on the "global variable" CurrentCulture. By passing the
cutlure as a parameter, I don't need a Try/Finally to restore the "global
variable"


The Try/Finally is not for the global variable setting however to catch
mistypings and throwed errors by the validation from the orderclass wich
can than be catched and processed by the sender of the DocumentToOrder
procedure.
CurrentCulture. Also I do not risk changing the "global variable"
CurrentCulture that routines that ConvertDocument ToOrder may call require
set in a specific way. Yes you want it changed for CDate, however you may
not want it changed for CDec or CInt! For example changing the decimal
separator from a comma to a period. Plus you may call one of your own


Obvious I want to change those, do you not know that they are part of
culture settings and in an ASPNET application would be than very
important.
routines (or team's routines) that requires the CurrentCulture to be a
specific value.


What I would do in my orderclass, with the culture however I thougth that
everybody would understand that.

I hope you agree tracking down problems with "global variables" in a
program can be very difficult, especially when they are being set & reset
all over the program, as opposed to being set once & left alone...


You are using them in every line. That is exact why I tell to use a global
setting and when that is build in, use that. .
All the formatting information needed by DateTime.Parse is contained in
the passed CutltureInfo object, As Herfried showed, DateTime.Parse will
notice that its a CultureInfo & actually use CultureInfo.Dat eTimeFormat
property. Likewise with CDec & CInt, if they need to follow the specific
culture, there is Decimal.Parse & Integer.Parse that accepts a
CultureInfo object.
o.price = Decimal.Parse(d .price, culture)
o.quantity =Integer.Parse( d.quantity, culture)

Again you avoid the avised methodes by Microsoft, can you give the
documentation where this is stated by Microsoft as better, it seems to me
code witch has very much change on errors.
Both Decimal.Parse & Integer.Parse will see the CultureInfo and actually
use CultureInfo.Num berFormat.

CDate, CDec, & CInt use the same CultureInfo object, they just don't
allow you to pass it directly, instead they rely on a global variable...
Maybe MS


I am very lucky with that, it is one of the basics from OOP programming
to narrowing everything as much as possible.
could simplify it for you & beginner VB.NET developers by allowing you to


Again I am not a beginner and it is not needed to simplify it for me, I
can everything understand what you and Herfried and Jon are writting.
However I use documentation you know not just empty writting, so show
that.

And again do not make the suggestion that I am a beginner. I have pasted
20 replies I think with an answer in the same trend, however I stuffed
them all, I do not go to that low level of arguing.
specify the format or culture on VB.NET's conversion functions. Rather
then try to explain why you might use DateTime.Parse or
DateTime.ParseE xact instead of the CDate. Would you understand it better
if you could do the following?


I showed you the Microsoft documentation, show yours. I think Microsoft
did a good job with that documtation, I do not understand your critique on
that.
Dim ci As New Globalization.C ultureInfo(d.cu lture)

o.orderdate = CDate(d.orderda te, ci)
o.deliverydate = CDate(d.deliver ydate, ci)
o.price = CDec(d.price, ci)
o.quantity = CInt(d.quantity , ci)


In other words: CDate, CDec, & CInt do not allow you to specify the
CultureInfo needed, I am suggesting using DateTime.Parse rather then
change a global variable/setting.


Again see my sentence by narrowing the change on errors.

By the way it was all the time DateTime.ParseE xact where you and Herfried
where talking about as the best way to go, Google archives everything you
know. .

Note: In your example, you are not restoring the original value of the
"global variable" CurrentCulture. At the very least save the current
CurrentCulture, then return the CurrentCulture to the one you save.

Dim startingCulture As CultureInfo =
Threading.Threa d.CurrentThread .CurrentCulture
Try
Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo(d.cu lture)
o.orderdate = CDate(d.orderda te)
o.deliverydate = CDate(d.deliver ydate)
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(myerr or, ex)
Return myError
Finally
Threading.Threa d.CurrentThread .CurrentCulture = _

startingCulture
End Try

Consider what happens if at the top of the Main routine the
CurrentCulture is set to a specific culture:
Shared Sub main()

' we have many different installed cultures on our network
' we need to insure that German - Switzerland is used all the time!
Threading.Threa d.CurrentThread .CurrentCulture =


Did you see that Thread.CurrentT hread.CurrentCu lture, dotNet is working
with threads which in themself are processing the steps, step by step so
it will be set in the end of my procedure evertime to the basic culture.
Or by setting it using the standard culture saved before when there is a
bug as Herfried wrote, that is really beginners work.
New Globalization.C ultureInfo("de-CH")

They are in the Net, you do not have to set them.
Dim d As New document


More then likely the above would be an ASP.NET app, and the
CurrentCulture is determined by the login routine, seeing as the ASP.NET
app is world wide, the CurrentCulture will be very different then the
InstalledUICult ure!


My sample is exatly made for ASPNET, it seems you did not read it, it
should work for documents from all over the world not only for Japan,
Germanny, the Netherlands, etc. where a culture setting is.

And in my sample you have nothing to do for it. (With the exception that
the IIS server variables are at the moment not sufficient for it, so we
have to wait on better ones, by instance the clientsertifica tes) and
therefore needs there to be field from the client at the moment where he
tells his language and country (by instance for Canada) what than can be
done in my opinon by an checked to a keys of an Idictionary implementing
class which gives than as value the culture.

I would say, try the sample and add some cultures, maybe than you
understand it better.

Cor

Nov 21 '05 #7
"Cor Ligthert" <no************ @planet.nl> schrieb:
Actually in Your Example I would use DateTime.Parse:

Instead of:
Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo(d.cu lture)
o.orderdate = CDate(d.orderda te)
o.deliverydate = CDate(d.deliver ydate)
I would use:
Dim culture As New Globalization.C ultureInfo(d.cu lture)

o.orderdate = DateTime.Parse( d.orderdate, culture)
o.deliverydate = DateTime.Parse( d.deliverydate, culture)


Why?


You snipped the explanation that goes directly after the quoted code in
Jay's post.
Microsoft advises to use the Microsoft.Visua lBasic conversion functions
for this.
No.
Do you know something that there is wrong in documentation from Microsoft
about this.? Or is there a piece of documentation I never saw. Until that
I relly on this from this page.
The quoted part of the documentation does not talk about cultures.
Notice that I am simply passing the culture required in the above, rather
then relying on the "global variable" CurrentCulture. By passing the
cutlure as a parameter, I don't need a Try/Finally to restore the "global
variable"


The Try/Finally is not for the global variable setting however to catch
mistypings and throwed errors by the validation from the orderclass wich
can than be catched and processed by the sender of the DocumentToOrder
procedure.


Setting the current culture is like setting a global variable. This
variable needs to be set back.
I hope you agree tracking down problems with "global variables" in a
program can be very difficult, especially when they are being set & reset
all over the program, as opposed to being set once & left alone...


You are using them in every line. That is exact why I tell to use a global
setting and when that is build in, use that. .


There is a difference. You snipped Jay's explanation.
o.price = Decimal.Parse(d .price, culture)
o.quantity =Integer.Parse( d.quantity, culture)

Again you avoid the avised methodes by Microsoft


Microsoft doesn't give the advice to use 'C*' functions even if their use is
semantically incorrect...
could simplify it for you & beginner VB.NET developers by allowing you to


Again I am not a beginner and it is not needed to simplify it for me, I
can everything understand what you and Herfried and Jon are writting.
However I use documentation you know not just empty writting, so show
that.


If you read the docs and took at look at how 'C*' methods are working in
matters of culture variance/invariance, then you would realize that the
'CDate' method calls 'Date.Parse' and passes the current culture to it. In
fact, we do /not/ want to work with the current culture but instead want to
use a certain culture. In order to be able to specify this specific culture
without changing the current culture we pass this culture in the culture
parameter of the 'Parse' method.
My sample is exatly made for ASPNET, it seems you did not read it, it
should work for documents from all over the world not only for Japan,
Germanny, the Netherlands, etc. where a culture setting is.


That's not the point. The point is that methods called by your
implementation will use the selected culture too, which is not always
desired.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #8
YS
If you just want to convert a string to a date using a certain culture, then
DateTime::Parse with the culture is the best bet. I'm not going to set the
thread culture just to do one call to CDate, which as Herfried said just
calls DateTime::Parse with the culture anyway. I vote with Herfried, Jay,
John.

YS

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Hello everybody,

Jay and Herfried are telling me every time when I use CDate that using the
datetime.parsee xact 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.parsee xact 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.parsee xact 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.parsee xact 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 = ConvertDocument ToOrder(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 = ConvertDocument ToOrder(d, o)
'Do the debugprinting yourself
End Sub
Public Shared Function ConvertDocument ToOrder(ByVal d As document, _
ByVal o As order) As Errors
Dim myError As New Errors
Try
Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo(d.cu lture)
o.orderdate = CDate(d.orderda te)
o.deliverydate = CDate(d.deliver ydate)
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(myerr or, ex)
Return myError
Finally
Threading.Threa d.CurrentThread .CurrentCulture = _
Globalization.C ultureInfo.Inst alledUICulture
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.parsee xact 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.

Nov 21 '05 #9
Did you read that page from Microsoft where I gave the link too?

It is obvious you did not, and with that are all answers on this message
given.

Cor
Nov 21 '05 #10

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

Similar topics

0
1606
by: Fred Strauss | last post by:
I am having an odd problem using the DateTime.ParseExact() method that I'm hoping someone can help me with. I have XML files that use 2 different date formats internally. I am trying to use the same call to ParseExact to parse either format. I am using C# to call the following form of ParseExact: public static DateTime ParseExact(string, string, IFormatProvider, DateTimeStyles);
3
7794
by: Dave P. | last post by:
I'm trying to parse a string into a DateTime object. I want to try using ParseExact to see if I can improve performance of a frequently-called method (i.e., please don't suggest using Parse()). My code looks like this: IFormatProvider fp = new System.Globalization.CultureInfo ("en-US"); DateTime dt = DateTime.ParseExact("Mar 9, 2005", "MMM d, yyyy", fp);
5
1750
by: ramon.smits | last post by:
I'm trying to get the following date/time string formatted. But I don't succeed. I get an exception saying the date/time string is invalid. string input = "8/09/05 9:27a"; string format = "M/dd/yy h:mmt"; DateTime.ParseExact(input, format, null); What am I doing wrong here? The following parse works ok.
1
4512
by: Pablo | last post by:
Hello, I have big problem with method ParseExact from class DateTime. Here is code sample: '----------------------------------------------------------------------------------------------------------------------------------------- Module Module1 Sub Main() Console.WriteLine("Start...") Try
1
15038
by: Erica | last post by:
Hi. I have a string: "11/24/2006 23:59" named StartDateTime I need the string to be converted into a DateTime object in the following format: 2006-11-24 11:59 PM I tried the following: DateTime.ParseExact(StartDateTime,"yyyy-MM-dd HH:mm tt*", System.Globalization.CultureInfo.CurrentCulture)
3
21283
by: Peter Duniho | last post by:
I'm sure there's a good explanation for this, but I can't figure it out. I tried using DateTime.Parse() with a custom DateTimeFormatInfo instance, in which I'd replaced the DateTimeFormatInfo.FullDateTimePattern property with my custom format string: DateTimeFormatInfo dtfi = (DateTimeFormatInfo)DateTimeFormatInfo.InvariantCulture.Clone(); dtfi.FullDateTimePattern = "dd/MMM/yyyy:HH:mm:ss zzz";
11
9855
by: Peter Holschbach | last post by:
Hi, I've the following line of code: result = DateTime.ParseExact("1999-12-01T23:59:59Z", "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture); where I get in result "result" "02.12.1999 00:59:59" instead of teh expected "01.12.1999 23:59:59" Anybody a idea ?
1
9582
by: =?Utf-8?B?RGFwcGVyRGFuSEBub3NwYW0ubm9zcGFt?= | last post by:
Given the example below, can someone explain why TimeSpan.TotalDays gives a different result than subtracting 2 DateTime.ToOADates? I am completely stumped. Thanks in advance, Dan Example Output: start: 1/1/2007 12:00:00 AM end : 1/1/2007 1:00:00 AM
5
5614
by: John B | last post by:
Hi all, Any idea why this code results in a FormatException? DateTime.ParseExact("40708", "dMMyy", CultureInfo.CurrentCulture) If I use "040708" with the same format string it works and it parses all double digit days fine. TIA JB
0
10643
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10705
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10330
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9477
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7879
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5716
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5907
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4113
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.