So I have simple example of my code and I can’t find idea how to rewrite it.
Code:
class Program
{
static void Main(string[] args)
{
var cat = new Cat();
cat
.ToFeed(arg => LikeMilk(1), c => c.Inc())
.ToFeed(arg => LikeMilk(2), c => c.Inc());
}
private static bool LikeMilk(int liters)
{
return liters <1;
}
}
public class Cat
{
private int Weith { get; set; }
public void Inc()
{
Weith++;
}
public Cat ToFeed(Func<int, bool> predicate, Action<Cat> action)
{
if (predicate) // Error: Cannot implicitly convert type 'System.Func<int,bool>' to 'bool'
action(this);
return this;
}
}
}
Of course I can change signature ToFeed to ToFeed(bool predicate, Action<Cat> action), but I don’t want to do it.
I understand that I have to add int parameter to call of predicate, but I have added it in Main()
How can I solve this problem?
Thanks
You’re passing a function value to ‘ToFeed’ and not a bool value. That’s why you need to actually invoke the function:
Edit:
Now that I look closer at your code, I see that you hard coded the amount of liters in the main function and that thus the lambda-parameter
argis useless.You should change the code in your main method to:
Now the lambda actually uses the parameter passed. Then you can add a
liters-Variable to yourCat-class which you pass to the predicate. See my updated code above.