views:

62

answers:

1

Hi,

I have the following code and it won't compile because the compiler cannot determine the return type of my CanExecute method. Can someone help me as to what is wrong?

class ViewCommand : ICommand
    {
        #region ICommand Members

        public delegate Predicate<object> _canExecute(object param);
        private ICommand _Execute;

        _canExecute exe;

        public bool CanExecute(object parameter)
        {
            return exe == null ? true : exe(parameter); // <-- Error no implicit conversion between Predicate<object> and bool
        }

... // more code
}
+1  A: 

The ICommand interface declares CanExecute as a function that takes a parameter and returns a bool.

Your _canExecute takes a parameter and returns a Predicate<object>

The way to invoke that would be to pass the parameter to the return value of exe

exe(parameter)(parameter);

I doubt that was your intention though.

I think you want to declare exe as a Predicate, and skip the delegate declaration.

private Predicate<object> exe;

This is what I think you want to look like:

class ViewCommand : ICommand
    {
        #region ICommand Members

        private ICommand _Execute;

        Predicate<object> exe;

        public bool CanExecute(object parameter)
        {
            return exe == null ? true : exe(parameter); // <-- Error no implicit conversion between Predicate<object> and bool
        }

... // more code
}
John Weldon
Now, I did your last, but it still not happy with the return types... what could be wrong?
Tony
what is the definition of the predicate you are assigning to _canExecute?
John Weldon
private Predicate<object> _canExecute(object param);
Tony
found the problem! The (object param) bit! The predicate itself already specifies the param type, so didn't need to do it again.
Tony
@Tony, you don't need to define a delegate for that, it already exists and it is Predicate<Object>. It doesn't make sense to return a predicate anyway, you need to return a bool
Thomas Levesque
excellent... thanks for the update.
John Weldon