我正在学习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
我做错了什么,怎样才能让它发挥作用?它应该看起来像一个简单的Windows窗体应用程序,它应该在右侧的标签中显示计算。我尽力遵循伪代码,但我不确定缺少什么,我一整天都在努力。这是我的代码:
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
有人可以提供一些指导或建议和意见吗?再一次,我对此非常陌生,非常感谢任何帮助!
答案 0 :(得分:6)
这是第83行:
decGross = intHours * decRate;
问题在于您的输入(即:数据类型,而不是您的键盘技能)。尽管您使用匈牙利表示法(which is officially discouraged, btw),但您的类型不正确。
decGross
定义为整数(Int32)
,当它应为Decimal
intHours
定义为Object
时应为整数decRate
被定义为Object
时应为Decimal
没有为Object
个实例定义乘法运算(因为它没有意义)。
虽然变量的基础值很可能是数字,但编译器不知道这一点,因为您已将它们键入Object
而不是Decimal
。 C#是静态类型的(在编译时)。我怀疑你来自VB背景,习惯于VB的后期绑定和可选的运行时类型检查,允许执行Variant
类型的乘法运算。
答案 1 :(得分:1)
1)您应该将intHours和decRate转换为任何数字类型
decGross = (int)intHours * (Decimal)decRate;
decGross应为数字
2)更好的方法是将intHours和decRate的类型更改为int和decimal,因为取消装箱。 https://msdn.microsoft.com/en-us/library/yz2be5wk.aspx
P.S。对不起,如果我的英语不好