简体   繁体   中英

C# error: 'object' does not contain a definition for 'Property' and no extension method 'property'

I get this error for every instance of a property used in this program:

'object' does not contain a definition for 'Property' and no extension method 'Property' accepting a first argument of type 'object' could be found.

What am I doing wrong?

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

The problem is that ArrayList contains objects as a object . I suggest to use List<Employee> instead of ArrayList

From msdn :

For a strongly-typed alternative to ArrayList, consider using List.ArrayList may not always offer the best performance for a given task.See the "Performance Considerations" section in the List reference topic for a discussion of the relative performance of these classes.

Just change

    static ArrayList employees = new ArrayList();

To

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

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