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