Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

overloading properties and Events

I have recently been overloading properties (and more recently events) so that I can apply various attributes to them (such as Browsable and EditorBrowsable).

I looked at the way microsoft did this with their standard components (using ILDasm) and found that the basically use code like this.
[Any necessary attributes here]
new/override Font Font //Use new or override depending on how the property was declared
{
get
{
      return base.Font;
}
set
{
      base.Font=value;
}
}

Anyway my question pertains to events as when Microsoft overload an event they seem to redeclare it as follows.
(Microsofts IL code is taken from Button and removing the Doubleclick event)

[Attributes here]
new public event EventHandler DoubleClick;

Mine and Microsofts IL code looks exactly the same with the event declaration.

What does differ is the Remove_DoubleClick and Add_DoubleClick. Microsofts simply call the base classes versions of these functions while mine do a bunch of stuff I dont understand except for a call to the System.Delegate Add/Remove function.

Anyway I was wondering if anyone could give me an example of how to overload an event in c# or direct me to a site that has an example. While my current version works (It hasn't caused an exception yet) I would like to all use microsofts approach as it seems more logical since I am just applying new attributes rather than code.

Anyway, any help is very much appreciated.

Luke Oliver
Wednesday, February 26, 2003

Sorry, I'm not sure I undrestand your question. What do you mean saying "overload an event"? Probably, you should read "Event Usage Guidelines" in Framework SDK Documentation:
ms-help://MS.NETFrameworkSDK/cpgenref/html/cpconeventusageguidelines.htm
[QUOTE:]
Use a protected virtual method to raise each event. The purpose of the method is to provide a way for a derived class to handle the event using an override. This is more natural than using delegates in situations where the developer is creating a derived class. The name of the method takes the form OnEventName, where EventName is the name of the event being raised. For example:
[C#]
public class Button
{
  ButtonClickHandler onClickHandler;
     
  protected virtual void OnClick(ClickEvent e)
  {
      // Call the delegate if non-null.
      if (onClickHandler != null)
            onClickHandler(this, e);     
  }
}

Mikhail Andronov
Thursday, February 27, 2003

Basically I am asking how to apply attributes to an event like how in the .NET framework the standard button class hides the DoubleClick event. I know to use the
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] attributes on the event, but doing so involves overloading the event, while I know how to do this, when I do it my results differ from the .NET frameworks IL Code.

I have found the following code produces code that is almost identical to the code that microsoft uses.

[Browsable(false),EditorBrowsable(EditorBrowsableState.Never)]
new public event EventHandler DoubleClick
{
    add
    {
    base.OnDoubleClick();
    }
    remove
    {
    base.OnDoubleClick();
    }
}

What I am trying to figure out now is how to pass the appropriate EventArgs parameter. The .NET framework simply uses the parameter passed to it by pushing it onto the stack. When I look at the IL Code my add/remove function also gets this parameter but I dont no how to use it. This is much easier to understand if you actually look at it in ILDasm and see how microsoft does it.

It would also be easier if I could corres[ond via e-mail since that way I can send source code to explain my point better.

My email is simonsays@dodo.com.au

Luke Oliver
Friday, February 28, 2003

Nevermind I figured it out. Ohh and the above was incorrect it should have been

[Browsable(false),EditorBrowsable(EditorBrowsableState.Never)]
public event EventHandler DoubleClick
{
    add
    {
        base.DoubleClick+=value;
    }
    remove
    {
        base.DoubleClick-=value;
    }
}

This produces the exact same IL Code as the .NET framework uses when it simply wants to change the attributes of an event.

I do have one question regarding all this though.

The .NET framework inherits its button class from Buttonbase not the Control class. I tried explicitly calling the add_DoubleClick methods of Control explicitly but VS.NET told that calling accessors directly was not allowed. One thing that could cause this behaviour would be code  since ButtonBase does not overload the DoubleClick method in anyway or use it (I checked this using ILDasm). If anyone has a definitive answer on this I would appreciate hearing about it.

Anyway thanks for your help.

Luke Oliver
Friday, February 28, 2003

*  Recent Topics

*  Fog Creek Home