Reputation: 1391
I am very new to C# and I have a problem with a homework. I have the following for a click event for some button. For the click event I want to call a function named functionA with a parameter named parameterB, a double type array. The additional parameter caused the error. What is the correct way for me to do this?
private void button1_Click(object sender, EventArgs e, double [] parameterB)
{
functionA(parameterB);
}
In the corresponding section of the Designer.cs
this.button1.Location = new System.Drawing.Point(386, 309);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(174, 27);
this.button1.TabIndex = 25;
this.button1.Text = "Calculate Final Grade";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
Upvotes: 1
Views: 4038
Reputation: 9726
The event handler no longer matched the definition when you tried to add another parameter. The EventHandler is defined with 2 paramters.
sender
Type: System.Object
The source of the event.
e
Type: System.EventArgs
An EventArgs that contains no event data.
You need to set parameterB as a property, or field of your form. You will then get parameterB from inside the event handler.
Upvotes: 0
Reputation: 726809
You do not call button1_Click
- it gets called when end-users click your button. The part of the system that detects button clicks knows how to inform your programs of the clicks using precisely one way: by calling a method with the signature
void OnClick(object sender, EventArgs e)
You cannot add to or remove arguments from the argument list, or change the return type of the method: it must match this signature precisely.
Now back to your parameterB
: you need it to make a call to the functionA
method, but it does not get passed to the event handler. How do you get it then? Typically, the answer is that you store it on the instance of the object that does event handling: declare an instance variable of type double[]
, set it when you have enough data to set it, and pass it to functionA
from inside your button1_Click
method.
Upvotes: 1