what is the java equivalent method "getSource()" in C# language
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(button1)){
//some code here
}
}
what is the java equivalent method "getSource()" in C# language
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(button1)){
//some code here
}
}
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
.
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
}
}
In .net handler is defined as follows
protected void btnname_event(
Object sender,
EventArgs e
)
{
//handler details
}
sender will be equaivalent of getsource()