Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

sum of two numbers

akmalcshakmalcsh Posts: 2Member
Hi
im new in C# so i want to write a program which will take two numbers from the user and add them.
I create a class and method and call it. but i wont run can you help me to find my fault here is my code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace SumNumbers_Method_Call
{
class sum
{
int result = 0;
public static int Sum(int firstNumber, int secondNumber) {
int result = firstNumber + secondNumber;
return result;

}
}
}
----------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SumNumbers_Method_Call
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int result = 0;
int32.TryParse

int calculate = sum.Sum(textBox1.Text,textBox2.Text);

MessageBox.Show("result is " + result);



}
}
}

Comments

  • Pelle-48Pelle-48 Posts: 43Member
    You need some kind of input.
    On Rosetta Code you can find some alternatives.
    Guess my number/ With feedback is one way to do it
  • andsydandsyd Posts: 2Member
    I think I see what might be wrong here.
    You're using the sum class variable result here:

    MessageBox.Show("result is " + result);

    But to me it looks like you should've used the calculate variable from:
    int calculate = sum.Sum(textBox1.Text,textBox2.Text);

    So it should read like:
    MessageBox.Show("result is " + calculate);

Sign In or Register to comment.