I'm adding a combobox to a WinForms application, I came across the DisplayMember & ValueMember properties. Their functionality is exactly what I need in my application, but I'm not sure I like the way they work.
comboBox.Items.Add(myObj);
comboBox.DisplayMember = "Name";
comboBox.ValueMember = "Id";
In the code above my object is the actual "Item", and it's name & Id properties will be used for displayed text & selected value respectively. The thing I'm unsure of is the hard coded "Name" & "Id" being passed into those properties. That is something not checked at compile time. So, if I ever changed my object.Name property to object.FullName, this code would break at runtime.
I've thought about getting the property name dynamically (as asked here: get name of a variable or parameter), but that feels very wrong.
So, is it ill-advised to use these properties? If so, what is the recommended way to store my object as the combobox item?