Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently using this method
C#
System.Convert.ChangeType(object value, Type conversionType, IFormatProvider provider); 

to convert object values into specific type. The problem is if value cannot be converted then it throws format exception. Since I need to do this for 1000 of entries it throws very considerable amount of exception hogging my CPU.

I have need to know if there is alternate way to convert where it provides a Try.. method option so I need not catch exception each time.
Posted
Updated 14-Jan-15 18:24pm
v2
Comments
BillWoodruff 15-Jan-15 4:32am    
I think Damith gave you great advice, but I also think the only really "big win" here ... if you expect a large percentage of objects in your dataset to not be convertable ... is for you to somehow iterate over the objects in the dataset before conversion and pass only those objects to the converter you think have a high chance of succeeding.

Of course that's easier said than done ! To answer the question if that strategy would be useful really requires an intimate knowledge of the objects that need to be converted, and any "patterns" in their contents that are definable.

1 solution

try by check using TypeConverter.CanConvertFrom Method[^] before using System.Convert.ChangeType[^] but I'm not sure it will gain performance improvement in your case. do a test and check actually how it affect with your inputs.
 
Share this answer
 
Comments
BillWoodruff 15-Jan-15 4:28am    
+5 Congratulations on being awarded the MVP, Damith !
DamithSL 15-Jan-15 4:45am    
Thank you and congrats :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900