Lutpy Lutpy - 1 year ago 101
C# Question

Cannot convert lambda expression & The name '_' does not exist in the current context

Can anybody help me to fix it?
i've got 2 type error message


  1. The name 'closure_' does not exist in the current context

  2. Cannot convert lambda expression to type 'System.Delegate' because
    it is not a delegate type



The code is:

private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
{
HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
string text = this.textBox5.Text;
htmlDocument.LoadHtml(text);
HtmlNodeCollection htmlNodeCollection = htmlDocument.DocumentNode.SelectNodes("//img/@alt");
int num1 = 0;
int k = 0;
if (htmlNodeCollection == null || this.backgroundWorker3.CancellationPending)
return;
string links = "";
foreach (HtmlNode htmlNode in (IEnumerable<HtmlNode>) htmlNodeCollection)
{
HtmlNode aTag = htmlNode;
int num2 = int.Parse(this.textBox7.Text);
this._busy.WaitOne(-1);
if (!this.backgroundWorker3.CancellationPending)
{
++k;
this.Invoke((Delegate) (() => this.richTextBox4.AppendText(k.ToString() + "." + Environment.NewLine + aTag.InnerHtml + aTag.Attributes["alt"].Value + Environment.NewLine + Environment.NewLine)));
++num1;
}
this.Invoke((Delegate) (closure_0 ?? (closure_0 = (Action) (() => links = this.richTextBox4.Text + Environment.NewLine))));
System.IO.File.WriteAllText(this.textBox2.Text + "/Descriptions.txt", links);
if (num1 == num2)
{
this.backgroundWorker3.CancelAsync();
if (!this.backgroundWorker3.CancellationPending)
this._busy.Reset();
}
}
}


Here is screenshoot

Thankyou

Answer Source

This line is useless code:

this.Invoke((Delegate) (closure_0 ?? (closure_0 = (Action) (() => links = this.richTextBox4.Text + Environment.NewLine))));

You can safely replace it with:

this.Invoke(new Action(() => { links = this.richTextBox4.Text + Environment.NewLine; }));

You first problem is that there is no variable or class field named closure_0 as far as the code you've shown.

Secondly, your syntax for creating an invoking an action on the UI is complicated and wrong, use the above simple approach.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download