Reputation: 77
I have code below. Is ther a way to write this more efficiently? Because i repeat the code, there are only other values to the class Drank.
private void btnCola_Click(object sender, RoutedEventArgs e)
{
Drank dranken = new Drank
{ Naam = "Cola", Prijs = 1.50M };
lblDrankje.Content = dranken.ToString();
GroupBox1.IsEnabled = false;
}
private void btnWater_Click(object sender, RoutedEventArgs e)
{
Drank dranken = new Drank
{ Naam = "Water", Prijs = 1.00M };
lblDrankje.Content = dranken.ToString();
GroupBox1.IsEnabled = false;
}
private void btnKoffie_Click(object sender, RoutedEventArgs e)
{
Drank dranken = new Drank
{ Naam = "Koffie", Prijs = 1.70M };
lblDrankje.Content = dranken.ToString();
GroupBox1.IsEnabled = false;
}
private void btnSoep_Click(object sender, RoutedEventArgs e)
{
Drank dranken = new Drank
{ Naam = "Soep", Prijs = 1.90M };
lblDrankje.Content = dranken.ToString();
GroupBox1.IsEnabled = false;
}
Upvotes: 1
Views: 18
Reputation: 2822
You could create a method with the logic for all click events
private void DrankClick(string naam, decimal prijs)
{
Drank dranken = new Drank
{ Naam = naam, Prijs = prijs };
lblDrankje.Content = dranken.ToString();
GroupBox1.IsEnabled = false;
}
and then have a method for each event e.g.
private void btnCola_Click(object sender, RoutedEventArgs e)
{
DrankClick("Cola", 1.50M);
}
Upvotes: 1