I'm just getting started in C# and hope you can help me choose the best way to pass values. I'm doing an exercise building a calculator in Visual Studio. I need to pass/get values once buttons are clicked.
I can see that a (sloppy?) way to do this would be to create code for each button's click event. Doing that, I would be certain which button is pressed... then could call a method and pass it a hard-coded argument indicating the button's value. For example, "BtnEntry_Click(1)" when the one button is pressed.
Another way would be to grab the sender's name, then have a switch statement that checks the button's name and assigns the value. For example:
private void BtnEntry_Click(object sender, RoutedEventArgs e)
{
var buttonClicked = sender as Button;
switch (buttonClicked.Name)
{
case "btnOne":
// The one button was pressed, so pass 1
break;
case "btnTwo":
//The two button was pressed, so pass 2
break;
//etc .....
}
}
A third option would be to put the value into the button's tag property, then grab it from there.
Is there another option using the RoutedEventArgs? If so, can you offer example code?
Is there a best way from among the options above... or is there a better way to do this?
Thanks!
Note: An editor noted that this question is related to the one posted at this question. I feel this is a broader view, addressing more potential solutions, and also easier for "noobs" like me to understand. The other question has stuff about binding code, which makes it an unhelpful answer for me.
I did, however, come across this answer regarding commands, which expands on the idea offered by Drew Noakes and kidshaw. (Thanks!)
So, I would suggest keeping this as a separate question, since it offers an easier entry point than the other and also addresses more options.