Home > Software design >  Method name expected and not null here C# error
Method name expected and not null here C# error

Time:12-31

I have this code snippet that set Images in list view control.

delegate void SetImageListDataCallback(string imgName, int i);

void SetImageListData(string imgName, int i)
{
    if (lvFoundModelImages.InvokeRequired)
    {
        SetImageListDataCallback c = new SetImageListDataCallback(imgName, i);
        this.Invoke(c, new object[]
        {
            imgName,i
        });
    }
    else
    {
        lvFoundModelImages.LargeImageList = imageList1;
        lvFoundModelImages.Items.Add(new ListViewItem(imgName, i));
    }
}

But the problem is I am getting errors in this line:

SetImageListDataCallback c = new SetImageListDataCallback(imgName, i);

in the first parameter,

ImgName is not null here

. while the second parameter "i" showing

method name expected

CodePudding user response:

You are using a wrong syntax to create a new instance of the SetImageListDataCallback. You should pass amethod with a matching signature as the first parameter of the delegate constructor:

SetImageListDataCallback c = new SetImageListDataCallback(SetImageListData);

Then, you can pass the imgName and i parameters as arguments at the invocation time:

this.Invoke(c, new object[] { imgName, i });
  • Related