views:

293

answers:

3

I am trying to use System.ComponentModel.TypeConverter to cast a bunch of System.Strings to different types. These Strings may or may not be in a valid format for the TypeConverter, so I'd like to find a way to check their validity before attempting the type conversion (to avoid having to rely on catching a System.FormatException to indicate that the String is not in the correct format).

Great, that's why TypeConverters have an IsValid() method, right? Well I'm running into a problem where IsValid() will return true, but when I call ConvertFromString(), it throws an exception. Here is some code to reproduce the issue:

        System.ComponentModel.DateTimeConverter DateConversion = new System.ComponentModel.DateTimeConverter();

        String TheNumberZero = "0";

        if (DateConversion.IsValid(TheNumberZero))
            Console.WriteLine(DateConversion.ConvertFromString(TheNumberZero).ToString());
        else
            Console.WriteLine("Invalid.");

When I run this, the line

Console.WriteLine(DateConversion.ConvertFromString(TheNumberZero).ToString());

throws a System.FromatException with the message

0 is not a valid value for DateTime.

What is the purpose of the IsValid() method if not to check the conversion input before attempting a type conversion? Is there some way I can check the String's validity short of having to catch the FormatException?

+1  A: 

The documentation is your friend:

The IsValid method is used to validate a value within the type rather than to determine if value can be converted to the given type. For example, IsValid can be used to determine if a given value is valid for an enumeration type. For an example, see EnumConverter.

You can write your own WillConvertSucceed method by wrapping the ConvertTo and ConvertFrom methods in exception blocks.

plinth
Also see https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=93559
Pavel Minaev
Does this mean there's no way to figure this out ahead of time without resorting to exception handling?
brian
Also, great link, thanks Pavel
brian
@plinth LOL! So, instead of fixing the a bug that was reported almost half decade ago, they changed the documentations?The original documentation http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.isvalid(VS.71).aspx states only:"Returns whether the given value object is valid for this type."So, infering from this statement, the IsValid function SHOULD tell us if any given value is valid within the context of ConvertFrom and ConvertTo methods.
Paulo Santos
@Pavel Minaev, @brian, @Paulo Santos: looks like they fixed that in .NET 4.0 (http://msdn.microsoft.com/en-us/library/tez461eb(v=VS.100).aspx)
Regent
Indeed, and this has been recorded as such in Connect, as well.
Pavel Minaev
A: 

Sounds like TryParse on Int32, DateTime, Decimal etc. might be a bit more useful and efficient.

ck
+1  A: 

This is a code example of what was suggested by ck.
Typically when you actually need to know if the parse of a value type will work this is the method to use.

        DateTime convertedDate;
        string zero = "0";

        if (!DateTime.TryParse(zero, out convertedDate))
        {
            throw new InvalidCastException(String.Format("Attempted Invalid Cast of {0} to DateTime",zero));
        }
Firestrand