简体   繁体   中英

C# Unable to multiply two figures together

Im trying to multiply a variable by another variable, who's value is stored in another class. In context, I'm trying to multiply an employees hours for the month by the hourly rate the employee gets paid by.

C# Employee class

Im getting the error in the method "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;
    }
}

Can someone explain what I'm missing here?

wage = hoursWorked * m_hourlyRate; ??

m_hourlyRate is object instance !

you need to use :

wage = hoursWorked * m_hourlyRate.m_hourlyRate;

Using Object Name same as an attribute inside the object is confusing. Change private HourlyRate m_hourlyRate to something else.

Edited :

Also , change : private decimal m_hourlyRate; to public decimal m_hourlyRate { set; get; } public decimal m_hourlyRate { set; get; } public decimal m_hourlyRate { set; get; } in order to access it.

OR

  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;
   }
}

then use :

wage = hoursWorked * m_hourlyRate.GetHourlyRate();

您不能将m_hourlyRate乘以必须使用此工资的整数= m_horulyRate.m_hourlyRate * hoursWorked;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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