Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating CheckedListBox with objects

Status
Not open for further replies.

JediDan

Programmer
May 15, 2002
128
0
0
US
Hello,

I'm trying to dynamically populate a CheckedListBox with objects that have a string property (name) and a numeric property (id). The control populates, but displays the name of my class (testproject.Player) even though I have a ToString() method in my class.

Here's the code:
Code:
//Class definition
public class Player
{
	public string playername;
	public int playernbr;

	public Player(string name,int id)
	{
	     this.playername = name;
	     this.playernbr = id;
	}

	public string ToString()
	{
		return this.playername;
	}
}

//Control population
Player p = new Player("Dan",0);

MessageBox.Show(p.ToString());			

clbPlayers.Items.Add(p,false);

The message box returns what we expect it to ("Dan").

Any help is much appreciated.
 
and it doesn't give you an error?

Try

protected override string ToString()

see if it can be overriden.
 
That did it! Actually, it had to be public override ToString(), but whatever.

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top