1

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);
Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
Andrew Lam
  • 1,371
  • 17
  • 35
  • Where do you get `parameterB` from? It won't be passed in to click, but if you tell us if it's in some text field or perhaps elsewhere in your program, we can help you figure out how to plumb it through to `functionA`. – sblom May 13 '12 at 03:44
  • Thank You for your help. The parameterB is a list of grades returned from another function after some calculation, lets say functionC. – Andrew Lam May 13 '12 at 03:50

2 Answers2

1

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • What does "set it when you have enough data to set it" means? This may sound like a stupid question, but What is the object that does event handling in my case....?Does it mean I have to declare the parameterB inside EventArgs? (How do I do that?) – Andrew Lam May 13 '12 at 06:24
  • Oh, After some tinkering, I think I get it now. I declared the local double[] variable outside of my original functionC. Now the subsequent methods can call on that variable now, From what I understand I would call that variable a "Global Variable" Right? But is it the right way to do it? maybe I am still missing the point. – Andrew Lam May 13 '12 at 06:45
  • @AndrewChiLam When you declare a variable outside a function, it is no longer *local*, but it does not become *global*: it becomes an *instance variable*. Instance variables are visible to all methods of your class, and each instance gets its own copy. You can further control access to that variable by declaring it `private`, `protected`, or `public` (in your case it should be `private`). Private instance variables are the main instrument of information hiding in OOP, so yes, this is the right way to go. – Sergey Kalinichenko May 13 '12 at 10:25
0

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.

Community
  • 1
  • 1
jac
  • 9,666
  • 2
  • 34
  • 63
  • How do I set parameterB as a property/field of the form? Sorry I am still confused – Andrew Lam May 13 '12 at 04:25
  • Please follow the link in field or go to http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c-sharp for a detailed explanation. – jac May 14 '12 at 14:43