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. Assigning EventHandler once in a method invoked many times

Assigning EventHandler once in a method invoked many times

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 2 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.
  • S Offline
    S Offline
    Seishin
    wrote on last edited by
    #1

    Hi! Is there a way to check if an event handler for an object has been already assigned in "current" object?! ex. class objOne{ public event EventHandler someEvent; } class objTwo{ public objOne; public event EventHandler someOtherEvent; } class objThree{ objThree(objTwo someObjectTwo){ someObjectTwo.someOtherEvent+=new EventHandler(someDelegate); } void someDelegate(object sender, EventArgs e){ //*** objTwo.objOne.someEvent+=new EventHandler(someOtherDelegate); } } someDelegate is invoked a few times and evey time it adds someOtherDelegate to the objTwo.objOne.someEvent.. my goal is to add this delegate only once.. the only idea that comes to me is to add a flag field in objOne specially for objThree but that takes away flexibility.. thanks for any help!!

    life is study!!!

    A 1 Reply Last reply
    0
    • S Seishin

      Hi! Is there a way to check if an event handler for an object has been already assigned in "current" object?! ex. class objOne{ public event EventHandler someEvent; } class objTwo{ public objOne; public event EventHandler someOtherEvent; } class objThree{ objThree(objTwo someObjectTwo){ someObjectTwo.someOtherEvent+=new EventHandler(someDelegate); } void someDelegate(object sender, EventArgs e){ //*** objTwo.objOne.someEvent+=new EventHandler(someOtherDelegate); } } someDelegate is invoked a few times and evey time it adds someOtherDelegate to the objTwo.objOne.someEvent.. my goal is to add this delegate only once.. the only idea that comes to me is to add a flag field in objOne specially for objThree but that takes away flexibility.. thanks for any help!!

      life is study!!!

      A Offline
      A Offline
      Arun Immanuel
      wrote on last edited by
      #2

      Seishin# wrote:

      public event EventHandler someEvent;

      Instead of simply declaring the delegate, assign null to it. public event EventHandler someEvent=null; check for null before adding to the delegate .

      Regards, Arun Kumar.A

      S 1 Reply Last reply
      0
      • A Arun Immanuel

        Seishin# wrote:

        public event EventHandler someEvent;

        Instead of simply declaring the delegate, assign null to it. public event EventHandler someEvent=null; check for null before adding to the delegate .

        Regards, Arun Kumar.A

        S Offline
        S Offline
        Seishin
        wrote on last edited by
        #3

        Hey!

        Arun.Immanuel wrote:

        check for null before adding to the delegate .

        i can check for null only in the class in which i declare the event.. i "resolved" the problem by trying -= the delegate form event before i += it.. thanks anyway

        life is study!!!

        A 1 Reply Last reply
        0
        • S Seishin

          Hey!

          Arun.Immanuel wrote:

          check for null before adding to the delegate .

          i can check for null only in the class in which i declare the event.. i "resolved" the problem by trying -= the delegate form event before i += it.. thanks anyway

          life is study!!!

          A Offline
          A Offline
          Arun Immanuel
          wrote on last edited by
          #4

          Seishin# wrote:

          objTwo.objOne.someEvent+=new EventHandler(someOtherDelegate);

          cant U check like: if(objTwo.objOne.someEvent==null) objTwo.objOne.someEvent+=new EventHandler(someOtherDelegate); This will allows only first assignment.

          Seishin# wrote:

          i "resolved" the problem by trying -= the delegate form event before i += it..

          Though this statement will make the delegate invoke only one function, U R remiving and assigning the function each time. Or can't u simply assign with "=" sign instead of +=, so that it will overwrite any existing one.

          Regards, Arun Kumar.A

          S 1 Reply Last reply
          0
          • A Arun Immanuel

            Seishin# wrote:

            objTwo.objOne.someEvent+=new EventHandler(someOtherDelegate);

            cant U check like: if(objTwo.objOne.someEvent==null) objTwo.objOne.someEvent+=new EventHandler(someOtherDelegate); This will allows only first assignment.

            Seishin# wrote:

            i "resolved" the problem by trying -= the delegate form event before i += it..

            Though this statement will make the delegate invoke only one function, U R remiving and assigning the function each time. Or can't u simply assign with "=" sign instead of +=, so that it will overwrite any existing one.

            Regards, Arun Kumar.A

            S Offline
            S Offline
            Seishin
            wrote on last edited by
            #5

            Arun.Immanuel wrote:

            cant U check like: if(objTwo.objOne.someEvent==null) objTwo.objOne.someEvent+=new EventHandler(someOtherDelegate);

            nope.. as i wrote you can check event for null only in the class where you declare it.. in this case it would by objOne.. in objThree I can only += or -= delegates..

            Arun.Immanuel wrote:

            Though this statement will make the delegate invoke only one function, U R remiving and assigning the function each time.

            yup.. and thus it'll prevent adding more than one instance of my wished delegate from objThree to someEvent.. if i didn't try to -= the delegate another instance of it would be added later and in effect in stead of caling the delegate once i'd do it as many times as i added it..

            Arun.Immanuel wrote:

            Or can't u simply assign with "=" sign instead of +=, so that it will overwrite any existing one.

            putting aside the fact that you cant do something like that, I'm assigning delegates to this event in other classes too so i can't do something like that.. btw. you shouldn't use the U in stead od 'you' etc. on this kind of forums (this kind of writing has its name but can't remember it :D )

            life is study!!!

            A 1 Reply Last reply
            0
            • S Seishin

              Arun.Immanuel wrote:

              cant U check like: if(objTwo.objOne.someEvent==null) objTwo.objOne.someEvent+=new EventHandler(someOtherDelegate);

              nope.. as i wrote you can check event for null only in the class where you declare it.. in this case it would by objOne.. in objThree I can only += or -= delegates..

              Arun.Immanuel wrote:

              Though this statement will make the delegate invoke only one function, U R remiving and assigning the function each time.

              yup.. and thus it'll prevent adding more than one instance of my wished delegate from objThree to someEvent.. if i didn't try to -= the delegate another instance of it would be added later and in effect in stead of caling the delegate once i'd do it as many times as i added it..

              Arun.Immanuel wrote:

              Or can't u simply assign with "=" sign instead of +=, so that it will overwrite any existing one.

              putting aside the fact that you cant do something like that, I'm assigning delegates to this event in other classes too so i can't do something like that.. btw. you shouldn't use the U in stead od 'you' etc. on this kind of forums (this kind of writing has its name but can't remember it :D )

              life is study!!!

              A Offline
              A Offline
              Arun Immanuel
              wrote on last edited by
              #6

              Seishin# wrote:

              btw. you shouldn't use the U in stead od 'you' etc. on this kind of forums (this kind of writing has its name but can't remember it )

              Thank you very much.

              Regards, Arun Kumar.A

              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