Reputation: 69
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:
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. 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!
Upvotes: 0
Views: 257
Reputation: 575
1)You should cast intHours and decRate into any numeric type
decGross = (int)intHours * (Decimal)decRate;
decGross should be numeric
2)Better way is to change types of intHours and decRate into int and decimal accordingly because of unboxing. https://msdn.microsoft.com/en-us/library/yz2be5wk.aspx
P.S. Sorry if my english is not well
Upvotes: 1
Reputation: 155578
This is line 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.
decGross
is defined as an integer (Int32)
when it should be a Decimal
intHours
is defined as an Object
when it should be an integerdecRate
is defined as an Object
when it should be a Decimal
The multiplication operation is not defined for Object
instances (as it's meaningless).
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
. C# is statically typed (at compile-time). 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.
Upvotes: 6