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
  1. Home
  2. General Programming
  3. C#
  4. event in c#

event in c#

Scheduled Pinned Locked Moved C#
csharphelpquestion
6 Posts 3 Posters 0 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
    tommmyyy123
    wrote on last edited by
    #1

    Hi, I have the code:

    using System;
    // Declare a delegate type for an event.
    delegate void MyEventHandler();
    // Declare a class that contains an event.
    class MyEvent
    {
    public event MyEventHandler SomeEvent;
    // This is called to raise the event.
    public void OnSomeEvent()
    {
    if (SomeEvent != null)
    SomeEvent();
    }
    }
    class EventDemo
    {
    // An event handler.
    static void Handler()
    {
    Console.WriteLine(“Event occurred”);
    }
    static void Main()
    {
    MyEvent evt = new MyEvent();
    // Add Handler() to the event list.
    evt.SomeEvent += Handler;
    if (evt.SomeEvent == null) Console.WriteLine(“Ev. nu are nici o metoda in lista”);
    // Raise the event.
    evt.OnSomeEvent();
    }
    }

    Why in the case of the second if (from Main) I have compiler error: error CS0070: The event ‘ConsoleApplication2.MyEvent.SomeEvent’ can only appear on the left hand side of += or -= (except when used from within the type ‘ConsoleApplication2.MyEvent’)

    And in the case of te first if it is OK ?

    tom

    P 1 Reply Last reply
    0
    • T tommmyyy123

      Hi, I have the code:

      using System;
      // Declare a delegate type for an event.
      delegate void MyEventHandler();
      // Declare a class that contains an event.
      class MyEvent
      {
      public event MyEventHandler SomeEvent;
      // This is called to raise the event.
      public void OnSomeEvent()
      {
      if (SomeEvent != null)
      SomeEvent();
      }
      }
      class EventDemo
      {
      // An event handler.
      static void Handler()
      {
      Console.WriteLine(“Event occurred”);
      }
      static void Main()
      {
      MyEvent evt = new MyEvent();
      // Add Handler() to the event list.
      evt.SomeEvent += Handler;
      if (evt.SomeEvent == null) Console.WriteLine(“Ev. nu are nici o metoda in lista”);
      // Raise the event.
      evt.OnSomeEvent();
      }
      }

      Why in the case of the second if (from Main) I have compiler error: error CS0070: The event ‘ConsoleApplication2.MyEvent.SomeEvent’ can only appear on the left hand side of += or -= (except when used from within the type ‘ConsoleApplication2.MyEvent’)

      And in the case of te first if it is OK ?

      tom

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Because that's how events work in C#. :-D Only the class that defines the event can access it that way; other classes can only add and remove handlers. However, a common practice with events is for the class to declare a Raise method that will check for null and raise the event. Ideally this method would be private or protected, but you could make it public if you really wanted to. If you are just learning about events that may make some sense, but it's generally not a good idea as it pretty much violates the whole idea of events. Furthermore, if that does make sense, maybe you just want a regular delegate rather than an event. We would need to know more about what you are trying to accomplish.

      T 1 Reply Last reply
      0
      • P PIEBALDconsult

        Because that's how events work in C#. :-D Only the class that defines the event can access it that way; other classes can only add and remove handlers. However, a common practice with events is for the class to declare a Raise method that will check for null and raise the event. Ideally this method would be private or protected, but you could make it public if you really wanted to. If you are just learning about events that may make some sense, but it's generally not a good idea as it pretty much violates the whole idea of events. Furthermore, if that does make sense, maybe you just want a regular delegate rather than an event. We would need to know more about what you are trying to accomplish.

        T Offline
        T Offline
        tommmyyy123
        wrote on last edited by
        #3

        And how I test the null handler list in Main : if (evt.SomeEvent == null) Console.WriteLine(“Event refered the null list”);

        G P 2 Replies Last reply
        0
        • T tommmyyy123

          And how I test the null handler list in Main : if (evt.SomeEvent == null) Console.WriteLine(“Event refered the null list”);

          G Offline
          G Offline
          GParkings
          wrote on last edited by
          #4

          you don't. think what you are trying to do here, you have a class called MyEvent which owns and exposes an event. That class is solely responsible for managing that event, other classes interacting with it are merely listeners The only point at which an event being null should matter is if you are about to invoke it. The only thing that invokes an event is its owner class and therefore only MyEvent needs to care about SomeEvent being null. Therefore if you want a console output for an attempt to invoke an event for which no event handler has been attached it should go in the OnSomeEvent method of the MyEvent class

          Pedis ex oris Quidquid latine dictum sit, altum sonatur

          1 Reply Last reply
          0
          • T tommmyyy123

            And how I test the null handler list in Main : if (evt.SomeEvent == null) Console.WriteLine(“Event refered the null list”);

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            You shouldn't, so you can't directly, at best the class can allow you to do so indirectly, but that's not how events are meant to work.

            T 1 Reply Last reply
            0
            • P PIEBALDconsult

              You shouldn't, so you can't directly, at best the class can allow you to do so indirectly, but that's not how events are meant to work.

              T Offline
              T Offline
              tommmyyy123
              wrote on last edited by
              #6

              Thanks

              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