Invalid cast errors and validation

Invalid cast errors and validation

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


boumbara posted on Saturday, September 29, 2007

Hello.

I'm new to CSLA, but like what I see so far.  I'm in the process of writing a couple of very simple apps and have lots of the key features working fine, but am stuck on this one issue. 

I have a property in a class which is an integer.  When I pass a non-numeric value to the property I very promptly get an invalid cast error, but I'd really like for my object to report 'invalid property [x] value must be an integer' if I pass in invalid data.  One of the things I most liked about CSLA while reading the book was that it allowed me to gather together all my validation code in one place (previously I used to put this all over the place but am trying to mend my ways!).  Two possible solutions to this problem resulting from my searching around on the subject are:

(i) Put validation into the UI to ensure I always pass the correct type through to the properties in my classes.  This could be via some kind of mask on the controls or code.

(ii) Change all the properties in my classes to strings and add in some extra validation rules to check that the values really are correct for the data type I really wanted to store.

Option (i) seems to break the neatness of having all the validation in one place.  

Option (ii) works (I tested that one out) but just feels wrong.  This was one of the few suggestions I found while searching around on the topic.  There were also a few threads regarding 'smart' data types, but I thought these were a means to handle empty values rather than non-empty and invalid values.

Anyway, I suspect there's an easy answer to all this.  Can someone tell me how its done?

Thanks

boumbara

Copyright (c) Marimer LLC