1

Closed

Null values with delegate command class

description

There is a problem when delegate command is created against value types (thus no null is allowed) the can execute and execute code should be like this:
 
 
/// <summary>
    /// Method executed to determine whether or not the command can execute in its current state.
    /// </summary>
    /// <param name="parameter">Information used by the command.</param>
    /// <returns>Returns true if this command can be executed, false otherwise.</returns>
    public bool CanExecute(object parameter)
    {
         var p = parameter == null ? default(T) : (T)parameter;
        return CanExecute(p);
    }
 
and
 
    /// <summary>
    /// The method to be executed when the command is invoked.
    /// </summary>
    /// <param name="parameter">Information used by the command.</param>
    public void Execute(object parameter)
    {
        var p = parameter == null ? default(T) : (T)parameter;
        Execute(p);
    }
 
this is because return CanExecute((T)parameter); does not work if T is value type and parameter is null ! cast null to int will cause exceptions.
same goes for the original Execute((T)parameter);
 
Can this be fixed? I am using a custom (read copy) of the delegate command to handle this case!
Closed Nov 30, 2011 at 6:53 AM by marlongrech
fixed thanks to Diego

comments