Click here to Skip to main content
16,010,650 members
Home / Discussions / C#
   

C#

 
GeneralRe: Serious bug in Array class Pin
leppie13-May-03 0:46
leppie13-May-03 0:46 
GeneralRe: Serious bug in Array class Pin
leppie13-May-03 9:13
leppie13-May-03 9:13 
GeneralRe: Serious bug in Array class Pin
James T. Johnson13-May-03 3:10
James T. Johnson13-May-03 3:10 
GeneralRe: Serious bug in Array class Pin
leppie13-May-03 9:20
leppie13-May-03 9:20 
GeneralRe: Serious bug in Array class Pin
Bo Hunter14-May-03 8:50
Bo Hunter14-May-03 8:50 
GeneralRe: Serious bug in Array class Pin
leppie14-May-03 9:35
leppie14-May-03 9:35 
GeneralFiring an event from outside the class Pin
Adam Turner12-May-03 22:55
Adam Turner12-May-03 22:55 
GeneralRe: Firing an event from outside the class Pin
James T. Johnson13-May-03 3:35
James T. Johnson13-May-03 3:35 
Adam Turner wrote:
Is there a way to fire an event in a class, from outside that class?

No, events can only be fired by the class that defines them. However, if you follow the recommended event pattern* then classes that inherit from ClassB could fire the event.

*The recommended patter is to define your event, MyEvent then also have a protected method called OnMyEvent(MyEventArgs e). In the class defining the event, OnMyEvent just fires the event.

If a class inherits from the one defining the event then there are three purposes for the OnMyEvent method. First is to handle the MyEvent event, without needlessly attaching a delegate to the event.

The second purpose ties in with the first, by not calling base.OnMyEvent(e); in your derived class you can prevent the event from firing. This is useful if you wish to restrict the times when the event is fired.

The third purpose can be to fire the event from within the derived class, this one is trickier because ensure that you don't break any implied behavior concerned with the event firing. To fire it, just call OnMyEvent and pass in the appropriate EventArgs object.

Of course, an example Smile | :)

class TheBase
{
  public event EventHandler MyEvent;
 
  protected virtual void OnMyEvent(EventArgs e)
  {
    if( MyEvent != null )
    {
      MyEvent(this, e);
    }
  }
}
 
class TheDerived
{
  protected override void OnMyEvent(EventArgs e)
  {
    if( ShouldFireMyEvent )
    {
      base.OnMyEvent(e);
    }
    else
    {
      // the MyEvent event will not be fired
    }
  }
 
  public void FireMyEvent()
  {
    // CAUTION: Fire MyEvent only if the code using this event is guaranteed
    // not to break because the Event was fired outside of its normal usage.
    //
    // IOW, if several events fire in a sequence, you must fire all of them
    // in that sequence.  Also don't fire this event yourself if you have
    // other means of doing so (such as calling Invalidate in a Windows
    // Forms Control instead of firing the Paint event yourself.)
    OnMyEvent(EventArgs.Empty);
  }
}
HTH,

James

"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation

GeneralHosting Windows Forms Control in IE, Please Help Pin
Nicholas Naddaf12-May-03 14:30
Nicholas Naddaf12-May-03 14:30 
GeneralRe: Hosting Windows Forms Control in IE, Please Help Pin
Kant13-May-03 7:24
Kant13-May-03 7:24 
GeneralEnvDTE + visual studio .net addin Pin
cbiacca12-May-03 13:49
cbiacca12-May-03 13:49 
GeneralRe: EnvDTE + visual studio .net addin Pin
Stephane Rodriguez.12-May-03 21:13
Stephane Rodriguez.12-May-03 21:13 
GeneralRe: EnvDTE + visual studio .net addin Pin
cbiacca13-May-03 1:26
cbiacca13-May-03 1:26 
GeneralRe: EnvDTE + visual studio .net addin Pin
Stephane Rodriguez.13-May-03 2:03
Stephane Rodriguez.13-May-03 2:03 
GeneralRe: EnvDTE + visual studio .net addin Pin
Stephane Rodriguez.13-May-03 2:04
Stephane Rodriguez.13-May-03 2:04 
GeneralRe: EnvDTE + visual studio .net addin Pin
cbiacca13-May-03 13:49
cbiacca13-May-03 13:49 
GeneralImageList(contd..) Pin
Kant12-May-03 12:50
Kant12-May-03 12:50 
GeneralRe: ImageList(contd..) Pin
James T. Johnson12-May-03 15:04
James T. Johnson12-May-03 15:04 
GeneralRe: ImageList(contd..) Pin
Kant12-May-03 15:25
Kant12-May-03 15:25 
GeneralEnumerating Top Level Windows Pin
Waleed Eissa12-May-03 12:12
Waleed Eissa12-May-03 12:12 
GeneralRe: Enumerating Top Level Windows Pin
leppie12-May-03 13:36
leppie12-May-03 13:36 
GeneralwinAPI functions Pin
datainjector12-May-03 12:06
datainjector12-May-03 12:06 
GeneralRe: winAPI functions Pin
Jon Newman12-May-03 12:20
Jon Newman12-May-03 12:20 
GeneralRe: winAPI functions Pin
Paresh Gheewala12-May-03 12:51
Paresh Gheewala12-May-03 12:51 
GeneralCoverting string to int32 Pin
datainjector12-May-03 11:34
datainjector12-May-03 11:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.