patterncsharpMinor
Crack the safe, find the right combination
Viewed 0 times
combinationthecracksafefindright
Problem
I recently started C# and I've got the feedback that I still repeat too much. How can I make it less repetitive?
XAML
namespace ExtraOefLabo
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string click = "";
private void btnOk_Click(object sender, RoutedEventArgs e)
{
//Input
//Proccess
//Output
if (click == "9382")
{
MessageBox.Show("Cracked the code!");
}
}
private void btnNr1_Click(object sender, RoutedEventArgs e)
{
click+= "1";
}
private void btnNr2_Click(object sender, RoutedEventArgs e)
{
click += "2";
}
private void btnNr3_Click(object sender, RoutedEventArgs e)
{
click += "3";
}
private void btnNr4_Click(object sender, RoutedEventArgs e)
{
click +="4";
}
private void btnNr5_Click(object sender, RoutedEventArgs e)
{
click += "5";
}
private void btnNr6_Click(object sender, RoutedEventArgs e)
{
click += "6";
}
private void btnNr7_Click(object sender, RoutedEventArgs e)
{
click += "7";
}
private void btnNr8_Click(object sender, RoutedEventArgs e)
{
click += "8";
}
private void btnNr9_Click(object sender, RoutedEventArgs e)
{
click += "9";
}
private void BtnNr0_Click(object sender, RoutedEventArgs e)
{
click += "0";
}
private void btnEnd_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
}
}XAML
Solution
XAML:
First I would recommend that you use the
Then the XAML code looks much cleaner and e.g. like this (but it is a bit longer):
In the image below you can see that the whole grid fills the GUI and will resize if you drag the window smaller / bigger.
Code behind (.cs):
As you can see in the code above i have deleted all the event handler, because they are unnecessary and can be implemented better. This was not a recursive programming fault. It was only a little bit too much code.
Recursion is e.g. calling a function
My C# code looks like this:
I only have one
Hint: Casting objects
In your special example it is very clear that the
First I would recommend that you use the
Grid.RowDefinitions and Grid.ColumnDefinitions more / better. Then all the mess with VerticalAlignment, Height etc. can be deleted. But maybe you want a specified Height then you still can do it:Then the XAML code looks much cleaner and e.g. like this (but it is a bit longer):
In the image below you can see that the whole grid fills the GUI and will resize if you drag the window smaller / bigger.
Code behind (.cs):
As you can see in the code above i have deleted all the event handler, because they are unnecessary and can be implemented better. This was not a recursive programming fault. It was only a little bit too much code.
Recursion is e.g. calling a function
Foo within the function Foo. The Recursion Wikipedia article may be a good first look into the chapter.My C# code looks like this:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string click = "";
private void ButtonOkClicked(object sender, RoutedEventArgs e)
{
if (this.click == "9382")
{
MessageBox.Show("Cracked the code!");
}
}
private void ButtonEndClicked(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void ButtonNumberClicked(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
this.click += (string)button.Content;
}
}
I only have one
Eventhandler for all the Buttons and I am reading the number from the Button which was clicked. The number is saved in its Content Property. You can read the clicked button with the sender parameter of the Eventhandler Method ButtonNumberClicked, but you have cast the sender to a Button because otherwise you are not able to access the Content property.Hint: Casting objects
In your special example it is very clear that the
sender from the ButtonNumberClicked must be a Button so you can cast it safe. But if it could be also a TextBox then you should cast it with the as operator and check for null:private void ButtonNumberClicked(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button != null)
{
this.click += (string)button.Content;
}
}
Code Snippets
<RowDefinition Height="50"/>Context
StackExchange Code Review Q#110895, answer score: 8
Revisions (0)
No revisions yet.