Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
Error   1   The best overloaded method match for 'Project_210.BubbleSorter.Sort(object[], Project_210.CompareOp)' has some invalid arguments    C:\Documents and Settings\Eddy Ho\Local Settings\Application Data\Temporary Projects\Project-210\Program.cs 67  13  Project-210
Error   2   Argument '2': cannot convert from 'Project_210.Program.CompareOp' to 'Project_210.CompareOp'    C:\Documents and Settings\Eddy Ho\Local Settings\Application Data\Temporary Projects\Project-210\Program.cs 67  42  Project-210

using System;
using System.Collections.Generic;
using System.Text;

namespace Project_210
{
    delegate bool CompareOp(object lhs, object rhs);

    class BubbleSorter
    {
        static public void Sort(object[] sortArray, CompareOp gtMethod)
        {
            for (int i = 0; i < sortArray.Length; i++)
            {
                for (int j = i + 1; j < sortArray.Length; j++)
                {
                    if (gtMethod(sortArray[j], sortArray[i]))
                    {
                        object temp = sortArray[i];
                        sortArray[i] = sortArray[j];
                        sortArray[j] = temp;
                    }
                }
            }
        }
    }

    class Employee
    {
        private string name;
        private decimal salary;

        public Employee(string name, decimal salary)
        {
                this.name = name;
                this.salary = salary;
         }
         public override string ToString()
         {
                return string.Format(name + " {0:C)", salary);
         }
         public static bool RhsIsGreater(object lhs, object rhs)
         {
                Employee empLhs = (Employee)lhs;
                Employee empRhs = (Employee)rhs;
                return (empRhs.salary > empLhs.salary) ? true : false;
         }
    }


    class Program
    {
        delegate bool CompareOp(object ihs, object rhs);

        static void Main(string[] args)
        {
            Employee[] employees =
                {
                    new Employee("Karli Watson",20000),
                    new Employee("Bill Gates",10000),
                    new Employee("Simon Robinson",25000),
                    new Employee("Mortime",(decimal)100000),
                    new Employee("Arabe Jones",20000),
                    new Employee("Avon from <code>'</code>;Black's 7'",50000)
                };
            CompareOp employeeCompareOp = new CompareOp(Employee.RhsIsGreater);
            BubbleSorter.Sort(employees, employeeCompareOp);

            for (int i = 0; i < employees.Length; i++)
                Console.WriteLine(employees[i].ToString());
        }
    }
}
Posted

1 solution

You have declared the CompareOp delegate in your namespace and in your class Program. The compiler sees them both and tells you he is unable to tell which of these two you actually mean to use even though they are practically the same. Simply remove your delegate type in the Program class.


Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900