Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CS8622 Nullability of reference types in type of parameter 'sender'

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.

like image 808
Kirsten Avatar asked Sep 14 '25 11:09

Kirsten


1 Answers

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}");
}
like image 62
Bartosz Olchowik Avatar answered Sep 15 '25 23:09

Bartosz Olchowik