2

what is the java equivalent method "getSource()" in C# language

public void actionPerformed(ActionEvent e) 
{ 
   if(e.getSource().equals(button1)){
      //some code here
     }
}
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
Ted Mad
  • 75
  • 2
  • 14

3 Answers3

7

There is not GetSource in C#. This is why, the UI events are usually using the signature (object sender, EventArgs eventArgs). The source is defined by the parameter sender.

Cédric Bignon
  • 12,892
  • 3
  • 39
  • 51
0

In .NET [C#/Vb.net]

The general signature of a EventHandler (delegate) is :

  public delegate void EventHandler(
      Object sender,
      EventArgs e
  )

Where : sender represents the : The source of the event.


So, Java Equivalent will be :

private void button1_Click(object sender, EventArgs e)
{
    if (Object.ReferenceEquals(sender, button1))
    {
        //wohoo!!! its the same object
    }
}
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
  • Thank you. How am i going to express that using the switch statement I want something like this: public void actionPerformed(ActionEvent e){ switch(e.getSource()){ case button1: // some code break; } } – Ted Mad Feb 03 '13 at 15:08
  • well .net will disappoint you a little, there is a limitation on switch case statement, you have to go with if else - if else logic! – Parimal Raj Feb 03 '13 at 15:16
  • http://stackoverflow.com/questions/44905/c-sharp-switch-statement-limitations-why - check this for more details! – Parimal Raj Feb 03 '13 at 15:17
0

In .net handler is defined as follows

protected void btnname_event(
      Object sender,
      EventArgs e
  )
{
//handler details
}

sender will be equaivalent of getsource()

user2031802
  • 744
  • 8
  • 7