I'm creating a lot of buttons on the back end so I figured it would be intelligent to abstract it out to cut down on lines of code. When I try to do this though, it gives me an error saying "A local or parameter named "buttonName" cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter". Am I missing something here? I thought this was the exact reason to use a parameter in this situation.
Here is an example of what I'm trying to do.
turn this
Button buttonDetailsEdit = new Button();
buttonDetailsEdit.ID = "ButtonDetailsEdit";
buttonDetailsEdit.Text = "Edit";
buttonDetailsEdit.UseSubmitBehavior = false;
buttonDetailsEdit.Click += new EventHandler(EditCall);
PlaceHolderDetailsContent.Controls.Add(buttonDetailsEdit);
Button buttonDetailsBack = new Button();
buttonDetailsBack.ID = "ButtonDetailsBack";
buttonDetailsBack.Text = "Back to List";
buttonDetailsBack.UseSubmitBehavior = false;
buttonDetailsBack.Click += new EventHandler(IndexCall);
PlaceHolderDetailsContent.Controls.Add(buttonDetailsBack);
Into this (with a method call in place of the old code)
void CreateButton(string buttonName, string buttonIDText, string buttonText, PlaceHolder PlaceHolderName, string methodCall)
{
Button buttonName = new Button();
buttonName.ID = buttonIDText;
buttonName.Text = buttonText;
buttonName.UseSubmitBehavior = false;
buttonName.Click += new EventHandler(methodCall);
PlaceHolderName.Controls.Add(buttonName);
}
the methodCall portion also throws an error here, I'm assuming due to "methodCall" not existing in this instance.
You can't have two variables string buttonName
and Button buttonName
with the same name in the same scope.
Try to rename your button to, for example, Button newButton
You have declared a parameter named buttonName
:
string buttonName
And you have declared a Button
variable of the same name:
Button buttonName
You cannot have both. This is what the compiler is complaining about.
Now, actually , your issue seems to be that you want to use the content of the parameter as a local variable name. This is generally not possible - variable names must be known at compile time, not only at runtime.
Luckily, you do not actually need that - it does not matter what your local Button
variable is called within your method, as that name will be lost when the method ends (actually, it will probably not even make it through compilation). The important part is that with each invocation of your method, a new Button
instance will be created (and ultimately added to the list).
Therefore, simply rename your Button
variable to newButton
and drop the buttonName
parameter because it isn't used any more.
The variable name buttonName
was reused and the type of methodCall
was incorrect. You need a delegate parameter to allow the selected function to be passed into the method.
void CreateButton(string buttonName,
string buttonIDText,
string buttonText,
PlaceHolder placeHolder,
EventHandler methodCall) // needed to fix your type
{
var button = new Button(); //reused variable name
button.ID = buttonIDText;
button.Name = buttonName; //you missed assigning the button name
button.Text = buttonText;
button.UseSubmitBehavior = false;
button.Click += methodCall; // you dont need to do new EventHandler
placeHolder.Controls.Add(button);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.