Sadashiva Madan
Sadashiva Madan

Reputation: 193

creating events in C # like in java

I have a class in java which basically performs a search in asynchronous and me returns the result through a listener, I need to write this same routine in C # I know there are some differences between java and therefore unable to write, I am beginner in c # I need at least a horizon.

public class Operation {

    private List<Operation.Listener> ListEventResult = new ArrayList<Operation.Listener>();     
    public void Search(String word){        
        try {
            Thread.sleep(3000);
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        if(ListEventResult.size()>0){
            for(Operation.Listener li : ListEventResult){
                li.Result("Result for "+word);                  
            }
        }
    }   
    public void addEventResult(Listener li){
        ListEventResult.add(li);
    }       

    public interface Listener{      
        public void Result(String result);
    }       
}

public class Program {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Operation op = new Operation();
        op.addEventResult(new Operation.Listener() {            
            @Override
            public void Result(String result) {         
                System.out.println(result);
            }
        });
        op.Search("Facebook");
    }
}

Upvotes: 1

Views: 92

Answers (1)

Tuyoshi Vinicius
Tuyoshi Vinicius

Reputation: 861

Events and Delegates

Events in C# are implemented with delegates. The publishing class defines a delegate. The subscribing class does two things: first, it creates a method that matches the signature of the delegate, and then it creates an instance of that delegate type encapsulating that method. When the event is raised, the subscribing class's methods are invoked through the delegate. A method that handles an event is called an event handler. You can declare your event handlers as you would any other delegate.

text-based http://msdn.microsoft.com/en-us/library/orm-9780596521066-01-17.aspx

You do not need a list of events as in java because the own C # handles events as a collection, making it possible to add them with the "+" operator

a simple example of your code in C #

class Operation
{
    public delegate void ResultHandler(object Operation, String result);
    public event ResultHandler Result;

    public void Search(String word) {

        Thread.Sleep(3000);
        if (Result != null)            
            Result(this, "Result for " + word);            
    }
}


public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Operation op = new Operation();
        op.Result += op_Result;
        op.Search("Facebook");

    }

    void op_Result(object Operation, string result)
    {
        MessageBox.Show(result);
    }        
}

Upvotes: 4

Related Questions