1

I have a class Employee, with some basic params, and a class employees that has a list of employee objects. I want to use the func delegate in the class employees to be able to use the following lambda expression in the main class. I think I have some syntax mistakes here... I'am getting the following error message: "cannot convert lambda expression to type 'Employee' because it is not a delegate type".

Someone knows what I'am doing wrong?

Many thanks guys !!

static void Main(string[] args)
        {
            Employees lijst = new Employees();
            lijst.Add(new Employee("B1", 200));
            lijst.Add(new Employee("B2", 100));
            lijst.Add(new Employee("B3", 300));

            lijst.Wijzig((Employee b) => b.Salary += 100);

            Console.ReadKey();
        }


class Employee
    {
        public String Name { get; set; }
        public int Salary { get; set; }



        public Employee(String Name, int Salary)
        {
            this.Name = Name;
            this.Salary = Salary;

        }
    }


class Employees
    {
        public Func<Employee, int> Wijzig = new Func<Employee, int>(Change);
        private ArrayList _lijst = new ArrayList();

        public void Add(Employee e)
        {
            _lijst.Add(e);
        }

        static int Change(Employee b)
        {

            return 0;
        }
    }
2
  • Yes indeed, but if I change Wijzig into Change, the problem is still the same... Commented Jun 16, 2017 at 11:28
  • 1
    What is your Wizjig supposed to do, anyway? What's its purpose? Commented Jun 16, 2017 at 11:31

3 Answers 3

2

Here is my solution that has no change in the Main function:

    static void Main(string[] args)
    {
        Employees lijst = new Employees();
        lijst.Add(new Employee("B1", 200));
        lijst.Add(new Employee("B2", 100));
        lijst.Add(new Employee("B3", 300));

        lijst.Wijzig((Employee b) => b.Salary += 100);

        lijst.Print();

        Console.ReadKey();
    }


    class Employee
    {
        public String Name { get; set; }
        public int Salary { get; set; }

        public Employee(String Name, int Salary)
        {
            this.Name = Name;
            this.Salary = Salary;
        }
    }


    class Employees
    {
        // [Delete]
        // public Func<Employee, int> Wijzig = new Func<Employee, int>(Change);
        private ArrayList _lijst = new ArrayList();

        public void Add(Employee e)
        {
            _lijst.Add(e);
        }

        // [Delete]
        //static int Change(Employee b)
        //{
        //    return 0;
        //}

        // [New]
        public void Wijzig(Func<Employee, int> func) 
        {
            foreach (Employee e in _lijst)
            {
                func(e);
            }
        }

        public void Print() 
        {
            foreach (Employee e in _lijst)
            {
                Console.WriteLine(e.Name + "\t" + e.Salary);
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

2

Wijzig is a delegate of type Func<Employee, int>. I.e. it's a method which accepts employee object and returns an integer. So lijst.Wijzig returns this delegate. If you want to invoke this delegate, you should pass employee object (according to signature Func<Employee, int>), but you are passing lambda delegate instead of an employee. Correct invocation:

  lijst.Wijzig(someEmployee);  // calls Employees.Change

That will invoke a int Change(Employee b) method. But seems like you don't want to invokde Change method (which does nothing, except returning zero). You want to invoke some real change - increment of salary. So, the first step you should do is change the delegate wich is assigned to Wijzig, and then invoke this delegate with your employee object:

 lijst.Wijzig = (Employee b) => { b.Salary += 100; return 0; }
 lijst.Wijzig(someEmployee); // calls lambda which we assigned

Notes:

  • there is no sense to use ArrayList in .NET 2.0+ you should use generic collection like List<Employee> instead.
  • it's not clear why your Change method returns an integer. Consider making it void.
  • you are re-implementing functionality LINQ - there is a ForEach extension method which accepts an action to be performed on each item of the collection.

        var list = new List<Employee> {
            new Employee("B1", 200),
            new Employee("B2", 100),
            new Employee("B3", 300)
        };
    
        list.ForEach(e => e.Salary += 100);
    

That's it. All your code without custom classes.

3 Comments

Many thanks for your reply !! Is there any way to not change this line of code :"lijst.Wijzig((Employee b) => b.Salary += 100);" but to change the "Change()" method instead?
@MathiasVerhoeven Change method cannot be changed - it is defined during compilation and stays there. But you can do the opposite - use Wijzig inside Change method. I'll give you an example.
@MathiasVerhoeven can you describe purpose of the Change method as you see it?
0

Your below code line to invoke func

lijst.Wijzig((Employee b) => b.Salary += 100);

should just be below, passing an Employee object

int result = lijst.Wijzig(lijst[0]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.