Recommendations for handling non-string values in objects.

Recommendations for handling non-string values in objects.

Old forum URL: forums.lhotka.net/forums/t/10480.aspx


MadGerbil posted on Wednesday, June 29, 2011

If I create an object with a property that is of type decimal and bind that as part of a collection of objects to a datagrid everything will work fine up until I place a non-numeric string value into the field at which point I receive an error.  For example, Amount="John".

Obviously, the runtime fails to convert the value 'John' to a decimal, hence the error.

In the past I've handled this by making all my exposed properties of type string.   I'd then write a low-priority rule that would check to make sure the string value could be converted to the required type.  If that worked all subsequent rules would cast the string as required for rule checking and I'd be clear to insert the value (upon cast) into the database.

Is there a better approach?

skagen00 replied on Wednesday, June 29, 2011

You mean like a textbox within a datagrid template column?

I'd use a numeric editor to stop the user from entering a non-decimal in the first place.  We ended up writing our own numeric control based on a googled one we saw that we liked the behavior of, which supports currency or non-currency and a "currency culture" property (i.e. fr-FR, en-US, etc).

(Sorry, can't share it, but it didn't take much more than a day worth of work and it's much better feedback to the user than allowing then to enter "John" for a salary amount for example).

(BTW you didn't mention UI but this was for SL that we did this)

Copyright (c) Marimer LLC