1

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.

Community
  • 1
  • 1
SH_ia
  • 29
  • 6
  • 1
    Take a look into using commands with the numeric buttons, and passing the value as a command parameter. There are many ways to do what you're after, but using command parameters is how I'd probably do it. – Drew Noakes Aug 23 '15 at 21:50
  • As per my answer, I'd agree with Drew. – kidshaw Aug 23 '15 at 21:51
  • possible duplicate of [Add parameter to Button click event](http://stackoverflow.com/questions/2006507/add-parameter-to-button-click-event) – khellang Aug 23 '15 at 21:55

2 Answers2

1

Of the options you offer, the tag would be my preference. But since this is wpf, you can implement an mvvm solution where you have a command and supply a parameter to a viewmodel.

Typically mvvm has overhead that might deter its use in small apps, such as a calculator. But since you're a newbie and looking for options - it's worth doing small to learn it.

kidshaw
  • 3,423
  • 2
  • 16
  • 28
  • Thanks! Can you tell me why you're not a fan of the tag property? – SH_ia Aug 23 '15 at 22:47
  • I think this relates to the other answer. The tag is a string value, which for your purposes should be fine. You're buttons impersonate keys on a calculator which have either a value or an operator, all of which have a character representation. The mvvm strategy will allow you to write unit tests against the underlying logic without even creating a UI. As I said, perfect tutorial project. – kidshaw Aug 24 '15 at 06:18
1

I'm not a fan of the Tag property because it is not typed. You might consider having a class inherit from Button, and have that class implement a property that exposes its underlying numeric value.

etcetera
  • 45
  • 3