【问题标题】:C# using IComparable<> and IComparerC# 使用 IComparable<> 和 IComparer
【发布时间】:2014-03-21 17:31:11
【问题描述】:

我在正确使用IComparable&lt;&gt; 接口时遇到了一些麻烦。我还创建了一个实现IComparer&lt;&gt; 的类。我几乎从书中复制了确切的编码示例,但对其进行了重新设计以适应我的情况,但它似乎无法正常运行。我不断收到错误消息类型名称EmployeeComparer 不存在于类型Lab4a.Employee 中。据我所知,该方法存在并且存在,所以我不确定是什么触发了这个。感谢您的任何建议

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab4a
{
    class Employee : IComparable<Employee>
    {
        private string _name = ""; // holds the employee name
        private int _number = 0; // employee's unique id number
        private decimal _rate = 0.0m; // employees pay rate
        private double _hours = 0.0d; // hours worked by the employee
        private decimal _gross = 0.0m; // gross pay for a single employee

        public Employee(string name, int number, decimal rate, double hours)
        {
            // initialize variables
            this._name = name;
            this._number = number;
            this._rate = rate;
            this._hours = hours;

            // if the employee works more than 40 hours, calculate the overtime pay
            if (_hours > 40)
            {
                double overtimeHours = _hours - 40; // calculates overtime hours worked
                decimal overtimePay = (_rate * 1.5m) * (decimal)overtimeHours; // calculates pay for overtime hours worked
                _gross = Math.Round((_rate * 40) + overtimePay, 2); // calculates regular pay plus overtime pay
            }
            else // otherwise, calculate regular pay
            {
                _gross = Math.Round(_rate * (decimal)_hours, 2);
            }
        }

        // Static method to get a comparer object
        public static EmployeeComparer GetComparer()
        {
            return new Employee.EmployeeComparer();
        }

        // Default CompareTo method
        public int CompareTo(Employee rhs)
        {
            return this._number.CompareTo(rhs._number);
        }

        // Special implementation to be called by custom comparer
        public int CompareTo(Employee rhs, Employee.EmployeeComparer.ComparisonType Which)
        {
            switch (Which)
            {
                case Employee.EmployeeComparer.ComparisonType.name:
                    return this._name.CompareTo(rhs._name);
                case Employee.EmployeeComparer.ComparisonType.empID:
                    return this._number.CompareTo(rhs._number);
                case Employee.EmployeeComparer.ComparisonType.rate:
                    return this._rate.CompareTo(rhs._rate);
                case Employee.EmployeeComparer.ComparisonType.hours:
                    return this._hours.CompareTo(rhs._hours);
                case Employee.EmployeeComparer.ComparisonType.gross:
                    return this._gross.CompareTo(rhs._gross);
            }
            return 0;
        }


        public override string ToString()
        {
            string employeeString = String.Format("{0,-20} | {1, -10} | {2, -7} | {3, -7:#.00} | {4, 10}", _name, _number, _rate, _hours, _gross);
            return employeeString;
        }

        public void PrintEmployee()
        {
            // displays employee data with proper spacing and divider bars
            System.Console.WriteLine(String.Format("{0,-20} | {1, -10} | {2, -7} | {3, -7:#.00} | {4, 10}", _name, _number, _rate, _hours, _gross));
        }
    }

    // Nested class which implements IComparer
    public class EmployeeComparer : IComparer<Employee>
    {
        public Employee.EmployeeComparer.ComparisonType WhichComparison { get; set; }

        // Enumeration of comparison types
        public enum ComparisonType
        {
            name,
            empID,
            rate,
            hours,
            gross
        };

        public int Compare(Employee lhs, Employee rhs)
        {
            return lhs.CompareTo(rhs, WhichComparison);
        }
    }
}

【问题讨论】:

  • EmployeeComparer 类移动到within Employee(嵌套)或从EmployeeComparer 的用法中删除“Employee.”。
  • 另外将Employee 设为publicEmployeeComparer 设为internal
  • 我应该提一下,我真的想不出一个很好的理由将 public 类嵌套到另一个类中。如果该类不打算在其父类上下文之外提供服务,为什么要公开它?

标签: c# icomparable icomparer


【解决方案1】:

EmployeeComparer 类型现在没有嵌套到 Employee 类中。如果你想要它嵌套在Employee类中移动它的声明。

【讨论】:

    【解决方案2】:

    在函数 GetComparer 你有

    public static EmployeeComparer GetComparer()
    {
        return new Employee.EmployeeComparer();
    }
    

    把它换成

    public static EmployeeComparer GetComparer()
    {
        return new EmployeeComparer();
    }
    

    Compiler 正在尝试寻找显然不存在的 Lab4a.Employee.EmployeeComparer。因为您的 EmployeeComparer 在 Lab4a 命名空间中,而不是在 Lab4a.Employee 命名空间中。

    【讨论】:

    • 有道理,我不敢相信我没有注意到这一点。非常感谢你的帮忙! :)
    【解决方案3】:

    这将起作用:

        public class Employee : IComparable<Employee>
        {
            private string _name = ""; // holds the employee name
            private int _number = 0; // employee's unique id number
            private decimal _rate = 0.0m; // employees pay rate
            private double _hours = 0.0d; // hours worked by the employee
            private decimal _gross = 0.0m; // gross pay for a single employee
    
            public Employee(string name, int number, decimal rate, double hours)
            {
                // initialize variables
                this._name = name;
                this._number = number;
                this._rate = rate;
                this._hours = hours;
    
                // if the employee works more than 40 hours, calculate the overtime pay
                if (_hours > 40)
                {
                    double overtimeHours = _hours - 40; // calculates overtime hours worked
                    decimal overtimePay = (_rate * 1.5m) * (decimal)overtimeHours; // calculates pay for overtime hours worked
                    _gross = Math.Round((_rate * 40) + overtimePay, 2); // calculates regular pay plus overtime pay
                }
                else // otherwise, calculate regular pay
                {
                    _gross = Math.Round(_rate * (decimal)_hours, 2);
                }
            }
    
            // Static method to get a comparer object
            public static EmployeeComparer GetComparer()
            {
                return new EmployeeComparer();
            }
    
            // Default CompareTo method
            public int CompareTo(Employee rhs)
            {
                return this._number.CompareTo(rhs._number);
            }
    
            // Special implementation to be called by custom comparer
            public int CompareTo(Employee rhs, EmployeeComparer.ComparisonType Which)
            {
                switch (Which)
                {
                    case EmployeeComparer.ComparisonType.name:
                        return this._name.CompareTo(rhs._name);
                    case EmployeeComparer.ComparisonType.empID:
                        return this._number.CompareTo(rhs._number);
                    case EmployeeComparer.ComparisonType.rate:
                        return this._rate.CompareTo(rhs._rate);
                    case EmployeeComparer.ComparisonType.hours:
                        return this._hours.CompareTo(rhs._hours);
                    case EmployeeComparer.ComparisonType.gross:
                        return this._gross.CompareTo(rhs._gross);
                }
                return 0;
            }
    
    
            public override string ToString()
            {
                string employeeString = String.Format("{0,-20} | {1, -10} | {2, -7} | {3, -7:#.00} | {4, 10}", _name, _number, _rate, _hours, _gross);
                return employeeString;
            }
    
            public void PrintEmployee()
            {
                // displays employee data with proper spacing and divider bars
                System.Console.WriteLine(String.Format("{0,-20} | {1, -10} | {2, -7} | {3, -7:#.00} | {4, 10}", _name, _number, _rate, _hours, _gross));
            }
        }
    
        // Nested class which implements IComparer
        public class EmployeeComparer : IComparer<Employee>
        {
            public EmployeeComparer.ComparisonType WhichComparison { get; set; }
    
            // Enumeration of comparison types
            public enum ComparisonType
            {
                name,
                empID,
                rate,
                hours,
                gross
            };
    
            public int Compare(Employee lhs, Employee rhs)
            {
                return lhs.CompareTo(rhs, WhichComparison);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 2022-10-12
      • 1970-01-01
      相关资源
      最近更新 更多