class Person {
// Field
private string name;
// Property
public string Name
{
get { return this.name; }
}
// ctor
public Person(string name)
{
this.name = name;
}
public override string ToString()
{
return this.name;
}
}
Programming tip: put your own objects in the user interface. By this we mean that you put objects themselves in the UI, not strings or other variables. For example, to add a Person object to a listbox:
listBox1.Items.Add(new Person("Sjakie"));
Use casting to retrieve the object from a UI control:
Person p = (Person)listBox1.Items[2];
Program a ToString()
method into all your classes
to ensure that you always have a good textual representation of your objects.