Valuemember/Displaymember vs. Delegates

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

I do not think it is good style to use these because you have to hardcode
the propertynamed in your code as string literals. Wouldn't it be better to
use delegates instead?

myComboBox.DisplayMember = new ReturnStringDelegate(myObj.Name);
myComboBox.Valuemember = new ReturnObjectDelegate(myObj.Value);
 
This would not work as the delegate refers to a property on a specific
object. This is not what you want since you want to call the same property
on a collection of objects.

Regards, Jakob.
 
Damn you are right. There should be some sort of static delegates which
define a method to call but not the object on which to call it.
 
Actually, this is what the types System.ComponentModel.PropertyDescriptor and
System.Reflection.PropertyInfo are capable of. But the only way to get a
hold of an instance of PropertyInfo for a property is to use Type.GetProperty
and the name of the property, which is exactly what ComboBox does (the base
class ListControl does this through the System.Windows.Forms.PropertyManager
class, I believe).

Regards, Jakob.
 
Back
Top