Home > database >  CS8622 Nullability of reference types in type of parameter 'sender'
CS8622 Nullability of reference types in type of parameter 'sender'

Time:04-21

I turned on TreatWarningsAsErrors in my net6.0-windows SDK project and am trying to solve the error

Nullability of reference types in type of parameter 'sender' of void myhander doesnt match the target delegate (possibly because of nullability attributes)

The code is

pricingBinder = new BindingSource() { DataSource = _pricingbo };
if (pricingBinder_DataError != null)
{
    pricingBinder.DataError -= pricingBinder_DataError;
    pricingBinder.DataError  = pricingBinder_DataError;
}

The event handler is

private void pricingBinder_DataError(object sender, BindingManagerDataErrorEventArgs e)
{
    throw new MyGeneralException("## pricingBinder_DataError {0} | {1}");
}

error message

I expect it has something to do with checking whether my event handler can be null but I am not sure how to do this.

CodePudding user response:

Its because BindingManagerDataErrorEventHandler requires nullable sender in definition. You can read about it here: BindingManagerDataErrorEventHandler

So you need to change your code from:

private void pricingBinder_DataError(object sender, BindingManagerDataErrorEventArgs e)
{
    throw new MyGeneralException("## pricingBinder_DataError {0} | {1}");
}

to

private void pricingBinder_DataError(object? sender, BindingManagerDataErrorEventArgs e)
    {
        throw new MyGeneralException("## pricingBinder_DataError {0} | {1}");
    }
  • Related