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);
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 signatureYou 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 thefunctionAmethod, 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 typedouble[], set it when you have enough data to set it, and pass it tofunctionAfrom inside yourbutton1_Clickmethod.