Reputation: 176
SO I am wanting to load a data source for a combo box with the use of a function that receives as a string the name of the data source it needs to load and then have it load it however I cannot get this to work as I think the program is simply trying to load the variable name rather that the data source it represents. Sorry if that's badly worded hopefully my code clears up what I mean.
This is how I am doing it now
bool TeamPlayers(string teamName, ComboBox team)//Makes the players of the selected team available for selection as scorers
{
if (teamName == "Canada")
{
string[] players = {"Johny Moonlight", "DTH Van Der Merwe", "Phil Mackenzie" };
team.DataSource = players;
}
else if (teamName == "New Zealand")
{
string[] players = {"Dan Carter", "Richie Mccaw", "Julian Savea" };
team.DataSource = players;
}
else if (teamName == "South Africa")
{
string[] players = {"Jean de Villiers", "Bryan Habana", "Morne Steyn" };
team.DataSource = players;
}
return (true);
}
But I would like to do something more like this
bool TeamPlayers(string teamName, ComboBox team)//Makes the players of the selected team available for selection as scorers
{
string[] Canada = {"Johny Moonlight", "DTH Van Der Merwe", "Phil Mackenzie" };
string[] NZ = {"Dan Carter", "Richie Mccaw", "Julian Savea" };
string[] RSA = {"Jean de Villiers", "Bryan Habana", "Morne Steyn" };
team.DataSource = teamName;
return (true);
}
Where teamName will be either Canada,NZ or RSA. Does anyone know of a way I can do this?
Upvotes: 0
Views: 76
Reputation: 9041
Make a Dictionary of team names.
Dictionary<string, string[]> teams = new Dictionary<string, string[]>();
public void PopulateTeams()
{
teams.Add("canada", new[] { "Johny Moonlight", "DTH Van Der Merwe", "Phil Mackenzie" });
teams.Add("nz", new[] { "Dan Carter", "Richie Mccaw", "Julian Savea" });
teams.Add("rsa", new[] { "Jean de Villiers", "Bryan Habana", "Morne Steyn" });
}
Usage of Dictionary:
private bool TeamPlayers(string teamName, ComboBox team)
{
team.DataSource = null;
if (teams.ContainsKey(teamName))
{
team.DataSource = teams[teamName];
return true;
}
return false;
}
Upvotes: 1
Reputation: 5771
You can use a dictionary to achieve similar functionality
bool TeamPlayers(string teamName, ComboBox team)//Makes the players of the selected team available for selection as scorers
{
Dictionary<string, string[]> teamNames = new Dictionary<string, string[]>();
teamNames.Add("Canada", new string[] { "Johny Moonlight", "DTH Van Der Merwe", "Phil Mackenzie" });
teamNames.Add("New Zealand", new string[] { "Dan Carter", "Richie Mccaw", "Julian Savea" });
teamNames.Add("South Africa", new string[] { "Jean de Villiers", "Bryan Habana", "Morne Steyn" });
team.DataSource = teamNames[teamName];
return (true);
}
Upvotes: 0
Reputation: 7470
You could make a dictionary like so:
dict<string, string[]> teamPlayers;
Key would be teamname, value would be players.
team.DataSource = teamPlayers[teamName];
Upvotes: 0