It's only been a few days since I started learning C#, but it doesn't seem like much progress.
So I'm trying to make a simple Windows Calculator application, but I can't exactly figure it out. 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 Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;
It's only been a few days since I started learning C#, but it doesn't seem like much progress.
So I'm trying to make a simple Windows Calculator application, but I can't exactly figure it out. 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 Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;
From the looks of the code, I would say that the OP is having trouble separating/parsing the input values into variableOne and variableTwo from one fieldbox.
OP, consider the actions you do when you type in something like 1 + 2 =
The user types in 1, presses add, types in 2 and presses equal.
Your code changes both variableOne and variableTwo at the same time with the same value which is incorrect.
private void FieldOne_TextChanged(object sender, EventArgs e)
{
double.TryParse(FieldOne.Text, out variableOne);
double.TryParse(FieldOne.Text, out variableTwo);
}
So I split up variableOne and variableTwo. But when I attempt to get an answer the result is always 0. 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 Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;
I've not done much winforms work, but looks to me like you are clearing the value in fieldone before you read it into variableTwo.
Second - I believe the TextChanged event is fired whenever the text is changed - either by the user or programmatically. There are many peices of code in your example which change the value of fieldOne - this TextChanged event will be fired after each of these occurences. It would seem that the final time this event is fired, fieldone has a value of 0.
I'm not too clear on your logic.
When do you expect variableOne to be read from the text box?
The way I would do it:
User insert value into fieldOne.
User clicks addition button
program reads value from fieldOne, puts into variableOne, clears fieldOne
User inserts second value into fieldOne
User clicks Equals button
program reads value from fieldOne, puts into variableTwo, clears fieldOne
program calculates answer
program inserts answer into fieldOne
Thanks Dave, that really helped.
So I got it to calculate correctly, but what if I were to do subtraction?
The way I currently have it setup is that when the user presses equal the two variables are added. How can I make it so that if the addition sign was clicked it does addition and same for subtraction?
-Edit-
So I attempted it myself, but now I'm wondering is there a more efficient way of doing this? 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 Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;
bool addition, subtraction, multiplication, division;
Thanks Dave, that really helped.
So I got it to calculate correctly, but what if I were to do subtraction?
The way I currently have it setup is that when the user presses equal the two variables are added. How can I make it so that if the addition sign was clicked it does addition and same for subtraction?
-Edit-
So I attempted it myself, but now I'm wondering is there a more efficient way of doing this? 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 Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;
bool addition, subtraction, multiplication, division;
One thing i've learnt in the few years i'v been programming - there are always more efficient ways of doing it. The most important thing is that it works.
If you want to try it though - you can use a delegate as a function pointer, ie - the function to be called can be set at runtime.
Code incoming - not tested.
namespace Calculator
{
public class OperatorFunctions
{
public delegate double OperatorDelegate(double n1, double n2); // here we declare a delegate function, which has a return type of double, and requires 2 doubles as parameters. We will use it to reference one of the functions below.
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;
OperatorFunctions.OperatorDelegate op; // here is our delegate object. it will hold a reference to one of the operator methods, although we don't know which one yet.
private void Addition_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
ClearAndFocus();
op = OperatorFunctions.Addition; // the delegate object op now stores a reference to the addition method
}
private void Subtraction_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
ClearAndFocus();
op = OperatorFunctions.Subtraction; // the delegate object op now stores a reference to the subtraction method
}
private void Multiplication_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
ClearAndFocus();
op = OperatorFunctions.Multiplication; // the delegate object op now stores a reference to the multiplication method
}
private void Division_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
ClearAndFocus();
op = OperatorFunctions.Division; // the delegate object op now stores a reference to the division method
}
private void Equals_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableTwo);
ClearAndFocus();
answer = op(variableOne, variableTwo); // we are now going to invoke the method referenced by this delegate object. But which method is it? Beats me, but we know it takes 2 doubles as parameters, and returns a double.
fieldOne.Text = answer.ToString();
}
}
}
I should also point out in the above code, there is a possible problem - if the user clicks the equals button without choosing an operation to perform - op will be unassigned. But hey, I did warn you it was untested
I was adding some new button like the actual numbers 0 through 9, and I also renamed some of the button. I also made sure to rename them inside my class file.
After doing this the button aren't doing what they're suppose to, in fact they do nothing at all.
If I add buttons and don't define what they do in my class file will that cause my whole program to break?