Hey guys,

I am new to C# and Object Oriented Programming,

I got this calculator code which was designed in Visual Studio 2008 and I am getting an error with

isZero == false

"Error Only assignment, call, increment, decrement, and new object expressions can be used as a statement."

Also if I remove the

isZero == false

I get an error saying that "Error Program 'C:\Users\Jordan McGehee\Documents\Visual Studio 2008\Projects\Calculator\Calculator\obj\Debug\Calculator.exe' does not contain a static 'Main' method suitable for an entry point"

Below is the code and I have attached project file

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 Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            isZero = true;
        }

        string oper;
        bool isZero;

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtCalculatorWindow.Text = "";
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            Button ClickedButton = (Button)sender;
            if (isZero == true)
            {
                txtCalculatorWindow.Text = ClickedButton.Text;
                isZero == false
            }

            else
                txtCalculatorWindow.Text += ClickedButton.Text;
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            Button ClickedButton = (Button)sender;
            if (isZero == true)
            {
                txtCalculatorWindow.Text = ClickedButton.Text;
                isZero == false
            }

            else
                txtCalculatorWindow.Text += ClickedButton.Text;
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            Button ClickedButton = (Button)sender;
            if (isZero == true)
            {
                txtCalculatorWindow.Text = ClickedButton.Text;
                 isZero == false
            }

            else
                txtCalculatorWindow.Text += ClickedButton.Text;
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            Button ClickedButton = (Button)sender;
            if (isZero == true)
            {
                txtCalculatorWindow.Text = ClickedButton.Text;
                isZero == false
            }

            else
                txtCalculatorWindow.Text += ClickedButton.Text;
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            Button ClickedButton = (Button)sender;
            if (isZero == true)
            {
                txtCalculatorWindow.Text = ClickedButton.Text;
                 isZero == false
            }

            else
                txtCalculatorWindow.Text += ClickedButton.Text;
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            Button ClickedButton = (Button)sender;
            if (isZero == true)
            {
                txtCalculatorWindow.Text = ClickedButton.Text;
                 isZero == false
            }

            else
                txtCalculatorWindow.Text += ClickedButton.Text;
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            Button ClickedButton = (Button)sender;
            if (isZero == true)
            {
                txtCalculatorWindow.Text = ClickedButton.Text;
                 isZero == false
            }

            else
                txtCalculatorWindow.Text += ClickedButton.Text;
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            Button ClickedButton = (Button)sender;
            if (isZero == true)
            {
                txtCalculatorWindow.Text = ClickedButton.Text;
                 isZero == false
            }

            else
                txtCalculatorWindow.Text += ClickedButton.Text;
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            Button ClickedButton = (Button)sender;
            if (isZero == true)
            {
                txtCalculatorWindow.Text = ClickedButton.Text;
                 isZero == false
            }

            else
                txtCalculatorWindow.Text += ClickedButton.Text;
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            Button ClickedButton = (Button)sender;
            if (isZero == true)
            {
                txtCalculatorWindow.Text = ClickedButton.Text;
                 isZero == false
            }

            else
                txtCalculatorWindow.Text += ClickedButton.Text;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            oper = "+";
        }

        private void btnSubtract_Click(object sender, EventArgs e)
        {
            oper = "-";
        }

        private void btnMultiply_Click(object sender, EventArgs e)
        {
            oper = "*";
        }

        private void btnDivide_Click(object sender, EventArgs e)
        {
            oper = "/";
        }

        private void btnEquals_Click(object sender, EventArgs e)
        {
            double res = 0;

            switch (oper)
            {
                case "+":
                    res = Convert.ToDouble(txtCalculatorWindow.Text);
                        break;
                case "-":
                        res = Convert.ToDouble(txtCalculatorWindow.Text);
                        break;

                case "*":
                        res = Convert.ToDouble(txtCalculatorWindow.Text);
                        break;
                case "\\":
                    
                    if (txtCalculatorWindow.Text == "0")
                        txtCalculatorWindow.Text = "Error";
                    else
                        res = Convert.ToDouble(txtCalculatorWindow.Text);
                        break;

            }


        }

        private void btnDecimal_Click(object sender, EventArgs e)
        {
           Button ClickedButton = (Button)sender;
           if (isZero == true)
           {
               txtCalculatorWindow.Text = ClickedButton.Text;
               isZero == false 
           }

           else
               txtCalculatorWindow.Text += ClickedButton.Text;
        }
    
    }


}

Edited 6 Years Ago by songweaver: n/a

The "==" is a comparing operator which returns true or false. Unlike the "=" operator that will assign a value to element.

Simply use == for comparison and = for assigning value.

Where exactly are talking about, the "0" is seems to be where the problem is but I can't figure it out.

The "==" is a comparing operator which returns true or false. Unlike the "=" operator that will assign a value to element.

Simply use == for comparison and = for assigning value.

Where exactly are talking about, the "0" is seems to be where the problem is but I can't figure it out.

with you if conditions there are lines that goes like this:

isZero == false;

This is not correct, this statement is a comparison. to assign a value to a variable do:

isZero = false;
Comments
Sometime's you have to break it down like that.

ok I got the problem with the ifZero, but now it gives me the error

"Error Program 'C:\Users\Jordan McGehee\Documents\Visual Studio 2008\Projects\Calculator\Calculator\obj\Debug\Calculator.exe' does not contain a static 'Main' method suitable for an entry point"

What is wrong? Again I am new with forms and OOP.

with you if conditions there are lines that goes like this:

isZero == false;

This is not correct, this statement is a comparison. to assign a value to a variable do:

isZero = false;

Edited 6 Years Ago by songweaver: n/a

Every forms application has automatically a Program.cs file that looks something like this:

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

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Did you removed it by accident from your project?
It contains the main entry point of your program: static void Main()

where does that need to go in my code I tried putting it in but I am getting a bunch of errors.


Every forms application has automatically a Program.cs file that looks something like this:

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

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Did you removed it by accident from your project?
It contains the main entry point of your program: static void Main()

Ok I got it to run however now I find it doesn't run properly, numbers input and decimal works, but operands don't + - * / =, I have attached the new project file with this post. Also I don't see code for the CE function, how would I go about doing that?

In this thread http://www.daniweb.com/forums/thread201807.html you will find a zip file somewhere with the code on how I did a calculator. Maybe it helps a little. Also look on this site for calculator you will find lots of other examples.
I also like to point out some troubles with your code:
The variable oper is undefined unless you clicked on a +,-,*,/ button. What would happen in the switch statement if you clicked on the equals button before you clicked an operator button?
You do not make any calculation, you just assign what is in your textbox to a variable res, which is local to the equals button click handler. After this handler finishes, res will no longer exist.
Enough to think about for the moment I should say. Untill soon!

Edited 6 Years Ago by ddanbe: n/a

This article has been dead for over six months. Start a new discussion instead.