Yesterday I got the question why you should use public properties and private variables instead of public variables.
Besides the fact that without a property wrapping the variable there is no way that you can detect when a change is made to the variable, there is another reason...DataBinding.
When binding an array of custom classes to e.g. ComboBox public variables as ValueMember and DisplayMember doesn't work.
Example
ComboBox1.DataSource = CustomClassArray; ComboBox1.ValueMember = "member1" //(member1 is public variable) ComboBox1.DisplayMember = "member2" //(member2 is public variable)
This results in a "Could not bind to the new display member" exception. Changing the custom class to expose properties instead solves the exception.
So use those props!
8:58:13 PM
|