0

Basically my question is how i add to a number like a calculator does for example? My code currently looks like this but it does a add operation instead of adding the number behind the existing number.

    private void button1_Click(object sender, RoutedEventArgs e)
    {
            value1 = value1 + 1;
            output = value1;
            textresult.Text = output.ToString();

if the user presses the button twice it would be 2. I want it to be 11. How do i do this?

  • 2
    `output = value1.ToString() + 1.ToString();` – Lei Yang Mar 27 '17 at 03:29
  • `output = value1 * 10 + 1`; – c.. Mar 27 '17 at 03:30
  • is the button they are pessing "1" and it shouldn't be "22" but "11"? If so, then the comments/answers will help, otherwise.... explain yourself a bit more – Keith Nicholas Mar 27 '17 at 03:33
  • @c.z. I think they still need to assign it to value1 as I'm guessing that's the member field they will need to keep updating – Keith Nicholas Mar 27 '17 at 03:35
  • @KeithNicholas Yes I think you might be right. In which case, what's the line `output = value1` doing? – c.. Mar 27 '17 at 03:58
  • @c.z. not a lot :) I've seen new programmers do that before though, make variables as holding spots representing their thinking, so they need a variable to do some magic stuff, then they need to output it. – Keith Nicholas Mar 27 '17 at 04:10

1 Answers1

1

You should use string variable instead int variable

int a = 1;
int b = 1;
int c = a+b;

The result of c is 2

string a = "1";
string b = "1";
string c = a+b;

If you use string it will be "11"

Erwin Kurniawan A
  • 958
  • 2
  • 9
  • 17
  • You may want to show how this would be used with a `TextBox` and whatever the OP is using –  Mar 27 '17 at 03:33