Properties validating

Topics: Validation Application Block
Apr 6, 2010 at 9:51 AM


I just discovered that in this code validation doesnt work in 4.0. Do you plan to add this possibility in future?

private string name;
public string Name { get { return name; } set { name = value; } }

Best wishes

Apr 6, 2010 at 11:31 AM

Hi rabbitsoft,

Private members are not designed to be accessible from outside your class, so allowing the validation block to access these would break encapsulation. In many cases you will create public properties that wrap your private fields, in which case the best approach is to apply the validation attributes to these properties.

Only members that have the following characteristics can be associated with a validator:
 They must be public members.
 Methods must be non-void and take no parameters.
 Properties must be readable.