Re: Delegates/events




"Peter Larsen []" <PeterLarsen@xxxxxxxxxxxxxxxx> wrote in message
news:u$hylEWbHHA.4872@xxxxxxxxxxxxxxxxxxxxxxx
Hi,

I have a problem passing an event in a method call.
Please see the following sample:


//This class holds information about listeners (in MyEvent).
public class First
{
public event EventHandler<EventArgs> MyEvent;
}


//This class could be a worker-thread.
public class Second
{
public void Method(First aInstance)
{
Third th = new Third();
th.AMethod(aInstance.MyEvent); //THIS IS where i dont know
what to do !!!!!!!!!
}

}


//This class do some work for the worker-thread and is supposed to notify
the listeners (in MyEvent) about the ongoing work.
public class Third
{
public void AMethod(EventHandler<EventArgs> callback)
{
if (callback != null)
callback(this, new EventArgs());
}
}


My problem is in class Second. How do i pass the event (in th.AMethod())
??

Is this what you mean?

public class First
{
public event EventHandler<EventArgs> MyEvent;
public void OnMyEvent(object sender, EventArgs args)
{
if(this.MyEvent != null)
this.MyEvent(sender, args);
}
}
public class Second
{
public void Method(First aInstance)
{
Third th = new Third();
th.AMethod(aInstance.OnMyEvent);
}
}
public class Third
{
public void AMethod(EventHandler<EventArgs> callback)
{
if(callback != null)
callback(this, new EventArgs());
}
}

PS


.



Relevant Pages

  • Re: How to Remove an Event Handler without knowing the name of the handler
    ... Public Class ClassA ... AddHandler MyEvent, AddressOf ClassBInstance.MyEventHandler ... >> Is there a way to dynamically remove an event handler from an event ... >> Public Sub AddHandler ...
    (microsoft.public.dotnet.languages.vb)
  • Delegates/events
    ... I have a problem passing an event in a method call. ... //This class holds information about listeners (in MyEvent). ... public class Second ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to use generics?
    ... getting rid of the ACollection and BCollection and just having Collectionwould be a good start towards using generics to the full extent. ... public void AddCollection() ... public bool ContainsKey ...
    (microsoft.public.dotnet.languages.csharp)
  • program challenge
    ... public void setName{ ... the salaried class that extends Employee ... public class Salaried extends Employee ... // Programmer.java: the programmer class that extends from Hourly ...
    (comp.lang.java.help)
  • Re: Tricky form problem
    ... > ShowDialog method) different modal child forms. ... when using ShowDialog() there's no ... > public class MainForm: Form ... > public void ShowForm() ...
    (microsoft.public.dotnet.framework.compactframework)