I'm trying to create ListBox where I will have key-value pair. Those data I got from class which provides them from getters.
Class:
public class myClass
{
private int key;
private string value;
public myClass() { }
public int GetKey()
{
return this.key;
}
public int GetValue()
{
return this.value;
}
}
Program:
private List<myClass> myList;
public void Something()
{
myList = new myList<myClass>();
// code for fill myList
this.myListBox.DataSource = myList;
this.myListBox.DisplayMember = ??; // wanted something like myList.Items.GetValue()
this.myListBox.ValueMember = ??; // wanted something like myList.Items.GetKey()
this.myListBox.DataBind();
}
It's similar to this topic [ Cannot do key-value in listbox in C# ] but I need to use class that returns values from methods.
Is it possible to do somewhat simple or I'd better rework my thought flow (and this solution) completely?
Thank you for advice!
The
DisplayMember
andValueMember
properties require the name (as a string) of a property to be used. You can't use a method. So you have two options. Change you class to return properties or make a class derived from myClass where you could add the two missing propertiesNow that you have made these changes you could change your list with the new class (but fix the initialization)