Enum conversion from an Object in VB.NET

  • Thread starter Thread starter Marlon
  • Start date Start date
M

Marlon

Which statement gives better performance (where obj can contain any of the
CLR runtime types)

1) CType([Enum].Parse(GetType(UploadMode), tmp), UploadMode)
or
2) Me._mode = CType(tmp, UploadMode)
or
3) System.ComponentModel.TypeDescriptor.GetConverter(
GetType(UploadMode)).ConvertFromxxxx(tmp)
 
Hi Marlon,

Thanks for your posting. I've also noticed your another thread in this
group.
As for converting object between types, I think the CType will always be
the preferred one since it just search to see whether it is possbile to
convert the source type to destination type, if not possible it failed and
the source and destination type don't need to have inheritance relation.
And if you're sure that the source type and the destination type has
inheritance relation, you can use
DirectCast keyword instead, that'll have better performance than CType ,
see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechar
t/html/vbtchperfopt.asp

In addition, as for the third option you mentioned

3) System.ComponentModel.TypeDescriptor.GetConverter(
GetType(UploadMode)).ConvertFromxxxx(tmp)

I'm not sure why we need to use such means, as far as I known , the
TypeDescriptor and TypeConvertor will use a lot of Reflection apis during
the convertion which may have performance concerns at runtime.

Thanks.

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.)
 
Back
Top