Reputation: 89
This is my c# code.I'm getting error in this particular line as Operator '&&' cannot be applied to operands of type 'bool?' and 'bool'
if (openFileDialog.ShowDialog() && viewModel.OpenFileCommand.CanExecute(openFileDialog.FileName))
{
viewModel.OpenFileCommand.Execute(openFileDialog.FileName);
}
Please help me to rectify this error.
Upvotes: 2
Views: 1461
Reputation: 81543
Returns
Nullable<Boolean>
A Nullable value of type Boolean that specifies whether the activity was accepted (true) or canceled (false). The return value is the value of the DialogResult property before a window closes.
The problem is the result from ShowDialog
is nullable.
However, you can rectify this by just using the more verbose == true
, the compiler then understands you want to explicitly know it's true, and not null or false. Don't ask me why it doesn't like the shorthand version (I'd have to dig through the specs).
Either way, here is your solution:
if (openFileDialog.ShowDialog() == true && viewModel....)
Upvotes: 4
Reputation: 4902
if (openFileDialog.ShowDialog() ?? false && viewModel.OpenFileCommand.CanExecute(openFileDialog.FileName))
Upvotes: 0