Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
namespace VisualCalculator
{
    /// 
    public partial class MainWindow : Window
    {

        bool isNewEntry = true;
        double currentValue = 0;
        enum Operation { Add, Subtract, Multiply, Divide, Equals, Start, LastOp };
        Operation currentOperation = Operation.Start;

        public MainWindow()
        {
            InitializeComponent();
            txtOut.Text = currentValue.ToString();
        }

        private void BtnEntry_Click(object sender, RoutedEventArgs e)
        {
            //dummy value for trying to parse the entry string
            int result;

            //Get the value from the button label
            Button btn = (Button)sender;
            string value = btn.Content.ToString();

            //special handling for decimal point
            if (value.Equals(".")) {
                if (isNewEntry)
                {
                    return;
                }
                if (!txtOut.Text.Contains(".")) {
                    txtOut.Text += value;
                    isNewEntry = false;
                }
                return;
            }

            //try to parse entry as int; 
            //if successful, append to current entry
            if (Int32.TryParse(value, out result))
            {
                if (isNewEntry || txtOut.Text.Equals("0"))
                {
                    txtOut.Text = "";
                }
                txtOut.Text += value;
                isNewEntry = false;
            }
        }

        private void Calculate(Operation op)
        {
            double newValue = Double.Parse(txtOut.Text);
            double result;

            if (op != Operation.LastOp)
            {
                currentOperation = op;
            }

            switch (currentOperation)
            {
                case Operation.Add:
                    result = currentValue + newValue;
                    break;
                case Operation.Subtract:
                    if (currentValue == 0)
                    {
                        result = newValue;    
                    }
                    else
                    {
                        result = currentValue - newValue;
                    }
                    break;
                case Operation.Multiply:
                    if (currentValue == 0)
                    {
                        result = newValue;    
                    }
                    else
                    {
                        result = currentValue * newValue;
                    }
                    break;
                case Operation.Divide:
                    if (newValue == 0)
                    {
                        txtOut.Text = currentValue.ToString();
                        return;
                    }
                    else if (currentValue == 0)
                    {
                        currentValue = newValue;
                        txtOut.Text = "0";
                        return;
                    }
                    else
                    {
                        result = currentValue / newValue;
                    }
                    break;
                default:
                    return;
            }

            currentValue = result;
            txtOut.Text = result.ToString();
            isNewEntry = true;
        }

        // 4 event handlers for operations:
        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            Calculate(Operation.Add);
        }
        private void BtnSubtract_Click(object sender, RoutedEventArgs e)
        {
            Calculate(Operation.Subtract);
        }
        private void BtnMultiply_Click(object sender, RoutedEventArgs e)
        {
            Calculate(Operation.Multiply);
        }
        private void BtnDivide_Click(object sender, RoutedEventArgs e)
        {
            Calculate(Operation.Divide);
        }

        //Clear the current results
        private void BtnClear_Click(object sender, RoutedEventArgs e)
        {
            txtOut.Text = "0";
            currentValue = 0;
            isNewEntry = true;
        }

        //Handle the Equals button
        private void BtnEquals_Click(object sender, RoutedEventArgs e)
        {
            Calculate(Operation.LastOp);
        }

    }
}


What I have tried:

Dear all,

I'm novice C# developer, and have a bit of problem understanding the code from Lynda.com tutorials. It's an application for a visual calculator.

I understand the btnEntry event handler but then I'm completely lost...

My questions are:
1) How does script diffrenciate from value typed "before" choosing an operator and "after"?
I noticed 2 vars - currentValue and newValue but completely don't get the logic how it's done...

2) The operation Calculate method with Operation argument:
Don't understand that one at all...

3) Multi (nested) "if statments"
What exactly does the return keyword do?

All the best,
Pete
Posted
Updated 17-Sep-16 11:57am
Comments
Patrice T 17-Sep-16 16:59pm    
And you never thought about asking the author ?
[no name] 17-Sep-16 17:10pm    
1 and 2 can easily be answered by you by learning how to use the debugger. #3 could easily be answered if you simply read the documentation.

1 solution

Quote:
1) How does script diffrenciate from value typed "before" choosing an operator and "after"?
I noticed 2 vars - currentValue and newValue but completely don't get the logic how it's done...


So your app is a calculator. If you understand how classes/objects work you'll see that currentValue is initialized to 0. Then once the mathematical operation is performed, the current value then becomes the result of that operation.

Example:

Current value = 0;
What i type into txtOut textbox is 1.
My operation is .Add

So then 1 + 0 = 1. Therefore based on the code you provided, current value is now 1.

It looks as though you have a button called clear which then resets the value of currentValue to 0 in your code.

Quote:
2) The operation Calculate method with Operation argument:
Don't understand that one at all...


The operation calculate method uses an enum to decide what mathematical operation it is performing. I am assuming you know basic calculator functions so you should be able to understand what something like Operation.Add means.

This parameter is used in a switch statement to execute conditionally what operation to perform.

Quote:
3) Multi (nested) "if statments"
What exactly does the return keyword do?


The return keyword exits execution of the code. If it is simply return; that is being used, it is because your method has a return type of void which means nothing is returned. If it was something like return 1;, then your method likely has a return type of int and you are returning 1 which is an integer
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900