Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A local or parameter named 'e' cannot be declared in this scope

Tags:

c#

.net

linq

I'm trying to use this code:

var controls = new[] { txtName, txtIdentityCard, txtMobile1 };
foreach (var control in controls.Where(e => String.IsNullOrEmpty(e.Text))) // error here in (e)
{
    errorProvider1.SetError(control, "Please fill the required field");
}

to check if one of my textboxes is empty, but it gives the following error:

A local or parameter named 'e' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

Any help?

like image 548
John Deck Avatar asked Dec 23 '22 14:12

John Deck


1 Answers

As error clearly saying you cannot use e because it is already used somewhere else, like in your event handler or somewhere else:

private void Form1_Load(object sender, EventArgs e)//Here for example

Try something else (c):

controls.Where(c => String.IsNullOrEmpty(c.Text)))

You also need to add this errorProvider1.Clear(); below the controls declaration.

like image 113
Salah Akbari Avatar answered Apr 06 '23 01:04

Salah Akbari