Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Event with no handler-method throws exception?

Event with no handler-method throws exception?

Scheduled Pinned Locked Moved C#
question
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TMattC
    wrote on last edited by
    #1

    Hi! I have defined an event in a certain class:

    public class BrokenPackageReceivedEventArgs : EventArgs
    {
    private readonly byte[] package;
    public byte[] Package { get { return package; } }
    public BrokenPackageReceivedEventArgs(byte[] _package) { package = _package; }
    }

    public event EventHandler BrokenPackageReceived;

    // Trigging the event:
    BrokenPackageReceived(this, new BrokenPackageReceivedEventArgs(dataLst.ToArray()));

    It seems that if no method is added to the event, it throws a NullReferenceException. I dont want that exception to occur, in fact I am baffled it actually does occur. Is this really the way events always do if no method is attached? Should I just use a try-catch with an empty catch to get rid of it, or am I missing something?

    T 1 Reply Last reply
    0
    • T TMattC

      Hi! I have defined an event in a certain class:

      public class BrokenPackageReceivedEventArgs : EventArgs
      {
      private readonly byte[] package;
      public byte[] Package { get { return package; } }
      public BrokenPackageReceivedEventArgs(byte[] _package) { package = _package; }
      }

      public event EventHandler BrokenPackageReceived;

      // Trigging the event:
      BrokenPackageReceived(this, new BrokenPackageReceivedEventArgs(dataLst.ToArray()));

      It seems that if no method is added to the event, it throws a NullReferenceException. I dont want that exception to occur, in fact I am baffled it actually does occur. Is this really the way events always do if no method is attached? Should I just use a try-catch with an empty catch to get rid of it, or am I missing something?

      T Offline
      T Offline
      TMattC
      wrote on last edited by
      #2

      I just realized I can do like this:

      if(BrokenPackageReceived != null)
      BrokenPackageReceived(this, new BrokenPackageReceivedEventArgs(dataLst.ToArray()));

      Is this the common way to do it?

      L P 2 Replies Last reply
      0
      • T TMattC

        I just realized I can do like this:

        if(BrokenPackageReceived != null)
        BrokenPackageReceived(this, new BrokenPackageReceivedEventArgs(dataLst.ToArray()));

        Is this the common way to do it?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Yes it is. And you could improve it a bit further by making a copy of it before you check it for null: var copy = BrokenPackageReceived; if(copy != null) copy(this, new .....); ..because theoretically some subscriber could unsubscribe during the execution of the null-check. edit: to clarify, not some subscriber but the last subscriber

        1 Reply Last reply
        0
        • T TMattC

          I just realized I can do like this:

          if(BrokenPackageReceived != null)
          BrokenPackageReceived(this, new BrokenPackageReceivedEventArgs(dataLst.ToArray()));

          Is this the common way to do it?

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Rather than doing this, try this:

          var handler = BrokenPackageReceived;
          if (handler != null)
          {
          handler(this, new BrokenPackageReceivedEventArgs(dataLst.ToArray());
          }

          The reason you will want to do this is that there's a potential race condition because it's possible for BrokenPackageReceived to have a value when you check it, and then be null when you attempt to call it.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups