Kasper Tito

Kasper Tito

  • NA
  • 1
  • 0

Conditional validation of fields in a Class

Feb 7 2009 5:54 PM

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!


Answers (1)