I’m fairly new to C# programming.
I am making a program for fun that adds two numbers together, than displays the sum in a message box. I have two numericUpDowns and a button on my form. When the button is pushed I want it to display a message box with the answer.
The problem is, I am unsure how to add the twp values from the numericUpDowns together.
So far, I have this in my button event handler:
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(this.numericUpDown1.Value + this.numericUpDown2.Value); }
But obviously, it does not work. It gives me 2 compiler errors: 1. The best overloaded method match for ‘System.Windows.Forms.MessageBox.Show(string) has some invalid arguments 2. Argument ‘1’: cannot convert decimal to ‘string’
Thanks!
this.numericUpDown1.Value + this.numericUpDown2.Valueis actually evaluating properly to a number, so you’re actually very close. The problem is that theMessageBox.Show()function, needs a string as an argument, and you’re giving it a number.To convert the result to a string, add
.ToString()to it. Like:For reference, if you want to do more advanced formatting, you’d want to use
String.Format()instead ofToString(). See this page for more info on how to useString.Format().