Search code examples
.netdata-bindingrefactoringstrong-typing

ListBox.DisplayMember = [String] Can I somehow workaround it to be other than string in .NET?


The question is asked with respect to an Object DataSource. So Consider that I have a class

public class Customer{

    public String name;
    public int age;

    public Customer(String name, int age) {
         this.name = name;
         this.age = age;
    }
}

And I have databound a list box to a list of these objects. So I say

listBox.DisplayMember = "name";

But my question is that when I refactor my Customer class's name to

public String fullName;

the DisplayMember still stays at "name". This will fail. So it decreases my ability to refactor domain objects. Is there any way around for this?


Solution

  • The only way i have found around this is to use extra properties on the objects so have a

    string DisplayMember
    {
        get { return 'name'; }
    }
    

    and when you refactor your object you then only need to change the returned string to your new property name making it a change in one location rather than in a few.

    It's not great but works better than hardcoding in the app!

    HTH

    OneSHOT