C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2720
Number of posts: 5746

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
sum of two numbers Posted by akmalcsh on 12 Nov 2012 at 3:52 AM
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);



}
}
}

Report
Re: sum of two numbers Posted by Pelle-48 on 12 Nov 2012 at 11:55 PM
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
Report
Re: sum of two numbers Posted by andsyd on 22 Jan 2013 at 6:12 AM
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);




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.