2

I am still trying to learn about c#, my question is how would I pass a variable from Item1_Click to Item2_Click, is this the same thing as passing them between methods or is this different because they are event handlers?

public partial class Events : System.Web.UI.UserControl
{
     protected void Item1_Click(object sender, EventArgs e)
    {

          //code

    }

    protected void Item2_Click(object sender, EventArgs e)
    {
          //code
    }
}
Bob1254
  • 75
  • 2
  • 7
  • I think it fair to say that you wouldn't *pass* a variable between the two methods (event handlers or not). You pass variables into methods and receive results, often as variables, back from them. What would you envisage passing between two click handlers when they may be called in any order and with any length of time between them? – Lazarus Mar 22 '11 at 12:54

7 Answers7

2

They are still regular methods, so you're still able to call them the same way you normally would.

 protected void Item1_Click(object sender, EventArgs e)
 {
    // Call the event handler for Item2, passing the arguments
    // Item1 received
    Item2_Click(sender, e);
 }

 protected void Item2_Click(object sender, EventArgs e)
 {
     // Make it happen.
 }

If you want to re-use Item1_Click just bind the click event of the other object to Item1_Click as well.

See the links below for some more information on events in C#.

Kevin
  • 5,626
  • 3
  • 28
  • 41
1

Event handlers are called by the publisher of the event. So you'd need to cache the value in a member variable if both handlers are in the same type. Item1 click caches something (e.g. the selection in a variable) and Item2 click uses this member variable for its own handling.

However nothing stops you from calling the event-handler#2 from event-handler#1 ; since it is a method after all. In this case, you could slot in the parameter in the EventHandler argument but it is a bit non-intuitive.

Gishu
  • 134,492
  • 47
  • 225
  • 308
0

What you've shown above, is a method. It's just that I imagine you've subscribed your methods to events on a couple of buttons.

It is then up to the buttons to populate the EventArgs instances themselves. If you wish to alter what goes into an EventArgs then you'd need to inherit from a Button and override the OnClick method to fire the event manually.

You could have some state information on your form (if you want shared information between the two methods). Or if you're literally wanting to pass information from Item1_Click to Item2_click then you can just call:

protected void Item1_Click(object sender, EventArgs e)
{
    this.Item2_Click(sender new EventArgs()); // <== Stick information in EventArgs
}
Ian
  • 33,605
  • 26
  • 118
  • 198
0

If you want to preserve some value from a first click you can set a variable and read it from your other handler.

 public partial class Events : System.Web.UI.UserControl
 {
      private SomeType variable;

      protected void Item1_Click(object sender, EventArgs e)
     {
           variable = someValue;
           //code
     }

     protected void Item2_Click(object sender, EventArgs e)
     {
           //code + do stuff with variable
     }
 }
Zebi
  • 8,682
  • 1
  • 36
  • 42
0

Event handlers are methods themselves, so no difference there. Sending data (variables if you will) between methods is done through parameters, however, Event handlers are required to have a specific signature, so you can't just add more parameters. The way to go here is to use a class member (field or property) as some sort of "global variable" (global to the class) as mentioned in @Zebi's answer

Hope this helps :)

AbdouMoumen
  • 3,814
  • 1
  • 19
  • 28
0

Instead of calling your event handler directly, you might want to create methods that wrap the functionality of Item1_Click and Item2_Click. For example..

public partial class Events : System.Web.UI.UserControl
{
     protected void Item1_Click(object sender, EventArgs e)
    {

          var theParam = GetParamValue();
          //code
          Item1Method(theParam);

    }

    protected void Item2_Click(object sender, EventArgs e)
    {               
           Item2Method();
    }

     private void Item1Method(object param1)
     {
        // Item 1 code goes here...
     }
     private void Item2Method()
     {
          //item 2 code goes here...
          // then call Item1Method
          var param2 = GetParamValue();
          Item1Method(param2);
     }
}

This is just an example of how to avoid calling your event handlers. Doing this will make your code more maintainable down the road.

In addition, now you don't have to worry about providing a sender and Event Args as parameters when trying to run the functionality in Item1_Click

Justin Largey
  • 1,934
  • 1
  • 16
  • 21
0

An event handler is just a method, that is called in some specific scenario. There's nothing to prevent you from explicitlly calling those methods, so

protected void Item1_Click(object sender, EventArgs e)     
{            
  Item2_Click(sender, e); //pass the original arguments
  Item2_Click(null, null); //pass some other arguments
  Item1_Click(null, null); //recursively call the handler.
} 

is perfectly valid C# code. However, it's a bad practice to use event handler for anything else than, basically handling the event. If two handlers need to use some same logic, it's better to do:

protected void Item1_Click(object sender, EventArgs e)
{
  CommonLogic();
}

protected void Item2_Click(object sender, EventArgs e)
{
  CommonLogic();
}

private void CommonLogic()
{
  //the common logic goes here
}
SWeko
  • 30,434
  • 10
  • 71
  • 106