About delegate and event.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven.Xu

    About delegate and event.

    Hello, everyone.
    As we know, some event definded like following:

    public delegate MyEventDelegate (object pram);
    public class ClassA
    {
    public event MyEventDelegate MyEvent;
    }

    My question is must I check the instance of "MyEvent" is exist before raise
    the event. For example:
    if (this.MyEvent != null) this.MyEvent(.. .);
    I don't think same situation in VB.NET.
  • Daniel O'Connell [C# MVP]

    #2
    Re: About delegate and event.


    "Steven.Xu" <StevenXu@discu ssions.microsof t.com> wrote in message
    news:6E6E4DC5-BD9E-4D9B-AF50-843DD86DE59E@mi crosoft.com...[color=blue]
    > Hello, everyone.
    > As we know, some event definded like following:
    >
    > public delegate MyEventDelegate (object pram);
    > public class ClassA
    > {
    > public event MyEventDelegate MyEvent;
    > }
    >
    > My question is must I check the instance of "MyEvent" is exist before
    > raise
    > the event. For example:[/color]
    Because the C# compiler basically treats accessing the event from within the
    defining class as accessing the underlying delegate field.
    [color=blue]
    > I don't think same situation in VB.NET.[/color]

    I don't think VB uses the same syntax to raise an event as it does to call a
    method, thus it is free to allow event invocation code to do a null check
    where the equivilent C# code would be surprising.

    C# could solve this by adding support for raise and generating a safe null
    check for you in trivial events.


    Comment

    Working...