C#错误:'object'不包含'Property'的定义,也没有扩展方法'property'

时间:2016-04-17 18:09:19

标签: c# properties

我为此程序中使用的每个属性实例都收到此错误:

  

'object'不包含'Property'的定义,也没有扩展名   方法'Property'接受'object'类型的第一个参数可以是   找到。

我做错了什么?

using System;
using System.Collections;
using static System.Console;

namespace CalculateGrossPay
{
public class CalculateGrossPay
{
    static ArrayList employees = new ArrayList();
    static int numOfEmployees = 0;

    static void main()
    {
        PrintMenu();
    }

    public static void PrintMenu()
    {
        string name;
        double annualSalary;
        double monthlySales;
        string input;

        WriteLine("Enter employee's name (enter nothing to quit): ");
        name = ReadLine();
        while (name != "")
        {
            WriteLine("Enter Annual Salary (0 for pure commission): ");
            input = ReadLine();
            annualSalary = double.Parse(input);
            WriteLine("Enter Monthly Sales: ");
            input = ReadLine();
            monthlySales = double.Parse(input);

            AddEmployee(name, GetBasePay(annualSalary), monthlySales);

            ReportPay();

            WriteLine("\nEnter employee's name (enter nothing to quit): ");
            name = ReadLine();
        }
    }

    public static double GetBasePay(double annualSalary)
    {
        return annualSalary / 12;
    }

    public static void AddEmployee(string name, double basePay, double monthlySales)
    {
        Employee employee = new Employee(name, basePay, monthlySales);
        employees.Add(employee);
        numOfEmployees++;
    }

    public static void ReportPay()
    {
        WriteLine(employees[numOfEmployees - 1].Name);
        WriteLine("Base Salary: {C:2}", employees[numOfEmployees - 1].BasePay);
        WriteLine("Sales: {C:2}", employees[numOfEmployees - 1].Sales);
        WriteLine("Commission: {C:2}", employees[numOfEmployees - 1].ReturnCommission(employees[numOfEmployees - 1].BasePay,
                            employees[numOfEmployees - 1].Sales));


        WriteLine("Gross Pay: {C:2}", employees[numOfEmployees - 1].ReturnGrossPay(employees[numOfEmployees - 1].BasePay, 
                            employees[numOfEmployees - 1].Sales));
    }

}

public class Employee {
    private string name;
    private double basePay;
    private double sales;

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    public double BasePay
    {
        get
        {
            return basePay;
        }
        set
        {
            basePay = value;
        }
    }

    public double Sales
    {
        get
        {
            return sales;
        }
        set
        {
            sales = value;
        }
    }

    public Employee(string nameOfEmployee)
    {
        name = nameOfEmployee;
    }

    public Employee(string nameOfEmployee, double pay, double monthlySales)
    {
        name = nameOfEmployee;
        basePay = pay;
        sales = monthlySales;
    }

    public double ReturnCommission(double basePay, double monthlySales)
    {
        double netSales = monthlySales - (10 * basePay);

        double commission = 0;

        if (netSales <= 0)
        {
            commission = 0;
        }
        else if (netSales > 50000)
        {
            commission = (netSales - 50000) * .2;
            commission += 25000 * .15;
            commission += 15000 * .1;
            commission += 10000 * .05;
        }
        else if (netSales > 25000)
        {
            commission = (netSales - 25000) * .15;
            commission += 15000 * .1;
            commission += 10000 * .05;
        }
        else if (netSales > 10000)
        {
            commission = (netSales - 10000) * .1;
            commission += 10000 * .05;
        }

        return commission;
    }

    public double ReturnGrossPay(double basePay, double monthlySales)
    {
        return basePay + ReturnCommission(basePay, monthlySales);
    }
}
}

1 个答案:

答案 0 :(得分:2)

问题是ArrayList将对象包含为object。 我建议使用List<Employee>代替ArrayList

来自msdn

  

对于ArrayList的强类型替代,请考虑使用List.ArrayList可能并不总是为给定任务提供最佳性能。请参阅List参考主题中的“性能注意事项”部分,以讨论这些任务的相对性能。类。

只需更改

    static ArrayList employees = new ArrayList();

    static List<Employee> employees = new List<Employee>();