Hi
I would like to perform some conditional validation on fields in a class and am not sure of the best way to do this.
These fields can be mandatory or optional depending on whether other fields (in the class) are set.
Here, I have a method which has an input parameter which is of type CQuery (a class):
response = ProcessQuery(CQuery)
I would like to validate this parameter and check that all required fields are set correctly.
Here are the fields in class CQuery:
Tenancy - this can be either Freehold or Leasehold
LengthOfLease - req' if Tenancy is Leasehold..
Landlord - req'd if Tenancy is Leasehold
PropertyValue - req'd if Tenancy is freehold.
The value for Tenancy, dictates which other field are mandatory or optional.
Is there a good way to do this without resorting to nested If else, If else, if else etc.. ?
I have other classes which are more complex and these would require some complex validation rules...and have many fields..
I have thought about serialising the class to XML at runtime and then perhaps validating the XML against a schema, but am not sure if this is the best way.
I have also considered using reflection, but then would still require some way to apply the validation rules.
thank you in advance!
Bechir BejaouiPosted Feb 11, 2009, 5:23 PM
Here an example focus on the data validation in this example
http://www.c-sharpcorner.com/UploadFile/yougerthen/612182008104124AM/6.aspx?ArticleID=b7e2caeb-6b37-4597-8715-694c2c30d971