most efficient way to convert data type

  • Thread starter Thread starter TS
  • Start date Start date
T

TS

I belive for primitive types, it is must efficient to use convert.toXXX()
instead of the ctype.

How does the DirectCast differ?

How does parse differ?

Say I have the following code to assign the value from a textbox to a
datetime variable:
dim x as datetime = txtDate.text

I'm wondering whats the best way to do this? I would normally do a
convert.ToDateTime(txtDate.text), but I also know there is a
date.parse(txtDate.text), and I"m not sure which is better.

thank You
 
Hi TS,

As for the "Convert.ToDateTime" and "DateTime.parse" , they are acutally no
difference. In fact, the Convert.ToDateTime will invoke the DateTime.parse
internally, here is the description in MSDN:
=========================
Remarks
The return value is the result of invoking the DateTime.Parse method on
value.

provider is an IFormatProvider instance that obtains a DateTimeFormatInfo
object. The DateTimeFormatInfo object provides culture-specific information
about the format of value. If provider is a null reference (Nothing in
Visual Basic), the DateTimeFormatInfo for the current culture is used.
=========================
For detailed info, you can refer to the following reference in MSDN:
#Convert.ToDateTime Method (String, IFormatProvider)
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemConvertClassTo
DateTimeTopic18.asp?frame=true

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Chances are, they all do the same thing "under the hood".

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Hi TS,

After some further searching, I've found the following definiation and
description on the two VB.NET specified CAST Expression(DirectCast and
CType)
#DirectCast
http://msdn.microsoft.com/library/en-us/vblr7/html/vakeyDirectCast.asp?frame
=true

#Cast Expressions
http://msdn.microsoft.com/library/en-us/vbls7/html/vblrfvbspec9_7.asp?frame=
true

It is mentioned that if both has the correct arguments and be called
successfully, the DirectCast has better performance than the CType.

In addtion, as for the Type.parse, I agree to Kevin's opinion that the
actual implement is under the hood and be
masked from us. My suggestion is that since the Type.parse is a runtime
function while the "DirectCast" is a language's directive, I'd perfer the
"DirectCast". Do you think so?


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top