C#无法将两个数字相乘

时间:2014-04-12 19:09:26

标签: c#

我试图将变量乘以另一个变量,其值存储在另一个类中。 在上下文中,我试图将该月的员工工时乘以员工获得的每小时工资。

C#员工类

我在方法" payHourly"

中收到错误
    class HourlyEmployee : Employee
{
    private HourlyRate m_hourlyRate;   //The amount an employee is to be paid per hour


    /// <summary>
    ///     Constructor
    /// </summary>
    /// <param name="p_employeeID"></param>
    /// <param name="p_employeeName"></param>
    /// <param name="p_employeeAge"></param>
    /// <param name="aRate"></param>
    public HourlyEmployee(int p_employeeID, string p_employeeName, int p_employeeAge, HourlyRate aRate)
    {
        m_employeeID = p_employeeID;
        m_employeeName = p_employeeName;
        m_employeeAge = p_employeeAge;

        m_hourlyRate.setEmployee(this);
    }

    public void payHourly(int hoursWorked)
    {
        int wage;
        wage = hoursWorked * m_hourlyRate;

    }
}

HourlyRate Class

    class HourlyRate
{
    private decimal m_hourlyRate;
    private HourlyEmployee m_employee;

    public HourlyRate(decimal p_hourlyRate)
    {
        m_hourlyRate = p_hourlyRate;
    }

    public void setEmployee(HourlyEmployee aEmployee)
    {
        m_employee = aEmployee;
    }
}

有人可以解释我在这里失踪的内容吗?

2 个答案:

答案 0 :(得分:1)

wage = hoursWorked * m_hourlyRate; ??

m_hourlyRate是对象实例!

你需要使用:

wage = hoursWorked * m_hourlyRate.m_hourlyRate;

使用与对象内部属性相同的对象名称令人困惑。 Change private HourlyRate m_hourlyRate其他内容。

已编辑:

另外,改变: private decimal m_hourlyRate;public decimal m_hourlyRate { set; get; }以便访问它。

  class HourlyRate
{
    private decimal m_hourlyRate;
    private HourlyEmployee m_employee;

    public HourlyRate(decimal p_hourlyRate)
    {
        m_hourlyRate = p_hourlyRate;
    }

    public void setEmployee(HourlyEmployee aEmployee)
    {
        m_employee = aEmployee;
    }

   public decimal GetHourlyRate()
   {
      return m_hourlyRate;
   }
}

然后使用:

wage = hoursWorked * m_hourlyRate.GetHourlyRate();

答案 1 :(得分:0)

你不能将m_hourlyRate乘以整数 你要用它 wage = m_horulyRate.m_hourlyRate * hoursWorked;