Question

Two function should be implemented, and I am wondering what type of validation I need to do on inputs and how to manage errors.

Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert

End Function

Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack

End Function

I need to check if the type of value and the type of parameter are what I am expecting.

In case they are not, should I return nothing and catch the wrong behavior in another place? Or should I throw here an exception or an assert?

Is there any best or suggested practices to apply here?

Was it helpful?

Solution

Personally i check for the type and value, then convert it. If for any reason it cannot be converted then i simply return value unconverted. I don't throw any exceptions or do any logging - most of the time it is easy to see if a conversion has failed because you don't see the expected item in the UI. Logging errors in a converter could be quite expensive, hence why i avoid it.

The data binding (usually) fails silently (i have had binding with a converter throw a big massive fit once in Silverlight), you can check the Output window for any errors, and it is pretty simple to whack a conditional breakpoint in the converter if necessary to debug any issues.

OTHER TIPS

I just wanted to find out why you need to catch an exception in the converter? Have you got an example?

In my opinion there should be no need for this, and with something like nullable types, these should be handled in the converter correctly, as everything else should be handled before the converter is called. I.e. if the user is entering a string into a numeric field, this should be caught by validation and not the converter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top