简体   繁体   English

为什么我的C#程序在我的简单税计算器应用中显示错误?

[英]Why does my C# program display an error for my simple tax calculator appclication?

I am learning C# and need to create a simple tax calculator for school using Visual Studio, and I think I followed most of the instructions, but I keep getting this error: 我正在学习C#,需要使用Visual Studio为学校创建一个简单的税费计算器,我想我遵循了大多数说明,但是仍然出现此错误:

Error   1   Operator '*' cannot be applied to operands of type 'object' and 'object'    C:\Visual Studio 2012\Projects\CS4\CS4Form.cs   83  32  CS4

What am I doing wrong and how can I get it to work? 我在做什么错,如何使它正常工作? It is supposed to look like a simple windows form app and it should display the calculations in the labels on the right. 它应该看起来像一个简单的Windows窗体应用程序,并且应该在右侧的标签中显示计算结果。 I did my best to follow the pseudo code but I am not sure what is missing and I have been working on it all day. 我尽力遵循伪代码,但是我不确定丢失了什么,我整天都在努力。 Here is my code: 这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

// CS4 
namespace CS4
{
    public partial class FrmCS4 : Form
    {
        public FrmCS4()
        {
            InitializeComponent();
        }
        // Declare class-level variables and constants
        // Class variables are initialized  to zero when declared

        int cintEmployeeCount;
        decimal cdecTotalNetpay;

        const decimal cdecFICA_RATE = 0.06M;
        const decimal cdecFEDERAL_RATE = 0.15M;
        const decimal cdecSTATE_RATE = 0.05M;
        const decimal cdecUNION_DUES = 10.00M;

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            // Declare medthod variables

            int decGross;
            decimal decFica;
            decimal decFederal;
            decimal decState;
            decimal decNetpay;

            // Input
            // Use nested try-catch blocks to get input values
            try
            {
                // Get hours worked from textbox 
                cintEmployeeCount = int.Parse(hoursWorkedBox.Text);
                try
                {
                    // Get pay rate from textbox 
                    cdecTotalNetpay = decimal.Parse(payRateBox.Text);

                    // Calculate gross amount 
                    decGross = intHours * decRate;
                    // Calculate taxes 

                    decFica = decGross * cdecFICA_RATE;
                    decFederal = decGross * cdecFEDERAL_RATE;
                    decState = decGross * cdecSTATE_RATE;

                    // Calculate net pay 
                    decNetpay = decGross - (decFica + decFederal + decState + cdecUNION_DUES);

                    // Accumulate summary values 
                    // Calculate average net pay 
                    cdecTotalNetpay += decNetpay;
                    cintEmployeeCount += 1;
                    decAverageNetpay = cdecTotalNetpay / cintEmployeeCount;


                    //Accumulate summary values 
                    //Calculate average net pay 

                    //Display results of calculations and summary values
                }
                catch (FormatException err)
                {
                    MessageBox.Show("Pay Rate must be numeric. " + err.Message,
                      "Data Entry Error", MessageBoxButtons.OK,
                      MessageBoxIcon.Exclamation);
                    payRateBox.SelectAll();
                    payRateBox.Focus();
                }
            }
            catch (FormatException err)
            {
                MessageBox.Show("Hours worked must be numeric. " + err.Message,
                  "Data Entry Error", MessageBoxButtons.OK,
                  MessageBoxIcon.Exclamation);
                hoursWorkedBox.SelectAll();
                hoursWorkedBox.Focus();
            }
            catch (Exception err)
            {
                MessageBox.Show("Unexpected Error: " + err.Message);
            }
        }//end method 



        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void label17_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void lblFederal_Click(object sender, EventArgs e)
        {

        }

        public object intHours { get; set; }

        public object decRate { get; set; }

        public decimal decAverageNetpay { get; set; }

        private void btnClear_Click(object sender, EventArgs e)
        {
            // Use Clear or null string "" for TextBoxes, but
            // only use null string "" for Labels

            hoursWorkedBox.Clear();   // Clear
            payRateBox.Clear();

            lblGross.Text = "";
            lblFica.Text = "";
            lblState.Text = "";
            lblFederal.Text = "";
            lblUnion.Text = "";
            lblNet.Text = "";
            lblTotalNet.Text = "";
            lblEmployee.Text = "";
            lblAverage.Text = "";


            //Reset Accumulators
            cdecTotalNetpay = 0;
            cintEmployeeCount = 0;
            decAverageNetpay = 0;

            hoursWorkedBox.Focus();
        }


        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
     // End of class
    // End of namespace 

Can anyone please provide some guidance or suggestions and opinions? 任何人都可以提供一些指导或建议和意见吗? Again, I am very new at this, and would greatly appreciate any help! 同样,我对此很陌生,非常感谢您的帮助!

This is line 83: 这是第83行:

decGross = intHours * decRate;

The problem is with your typing (that is: data types , not your keyboard skills). 问题出在您的键入(即: 数据类型 ,而不是您的键盘技能)上。 Despite your use of Hungarian Notation ( which is officially discouraged, btw ) your types are incorrect. 尽管您使用的是匈牙利符号( 官方不建议使用btw ),但是您的类型不正确。

  • decGross is defined as an integer ( Int32) when it should be a Decimal decGross应该是Decimal时定义为整数( Int32)
  • intHours is defined as an Object when it should be an integer intHours应该为整数时,它被定义为Object
  • decRate is defined as an Object when it should be a Decimal decRate在应为Decimal时定义为Object

The multiplication operation is not defined for Object instances (as it's meaningless). 没有为Object实例定义乘法操作(因为它没有意义)。

While the underlying values of the variables may very-well be numeric, the compiler does not know this because you've typed them as Object rather than Decimal . 尽管变量的基础值很可能是数字,但编译器不知道这一点,因为您将其键入为Object而不是Decimal C# is statically typed (at compile-time). C#是静态类型的(在编译时)。 I suspect you come from a VB background and are used to VB's late-binding and optional runtime type-checking where performing multipliction of Variant types is allowed. 我怀疑您来自VB背景,并且习惯于VB的后期绑定和可选的运行时类型检查,其中允许执行Variant类型的乘法。

1)You should cast intHours and decRate into any numeric type 1)您应该将intHours和decRate转换为任何数字类型

decGross = (int)intHours * (Decimal)decRate;

decGross should be numeric decGross应该是数字

2)Better way is to change types of intHours and decRate into int and decimal accordingly because of unboxing. 2)更好的方法是因为取消装箱,将intHours和decRate的类型相应地更改为int和十进制。 https://msdn.microsoft.com/en-us/library/yz2be5wk.aspx https://msdn.microsoft.com/zh-CN/library/yz2be5wk.aspx

PS Sorry if my english is not well PS对不起,如果我的英语不好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM