Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Removing events more than once

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MikeP - 21 Sep 2007 14:10 GMT
Hi there,

I've tried hunting for this in the C# specs but can't find the relevant
info. Does anyone know what the official rule says (or doesn't say) about
removing an event more than once. If you apply "+=" to assign an event for
instance, can you safely apply "-=" more than once or is this implementation
dependent (and possibly dangerous). Thanks very much
Ignacio Machin ( .NET/ C# MVP ) - 21 Sep 2007 15:04 GMT
Hi,

> Hi there,
>
[quoted text clipped - 3 lines]
> instance, can you safely apply "-=" more than once or is this
> implementation dependent (and possibly dangerous). Thanks very much

I haven't read the documentation, but if they used the same pattern than for
the rest of the list I can tell you that it does not matter, that it will
removed if found and nothing happens if it does not.
Michael Nemtsev, MVP - 21 Sep 2007 15:11 GMT
Hello Ignacio Machin ( .NET/ C# MVP )" machin TA laceupsolutions.com,

Yep, exactly.

C# Spec. 15.4
Attempting to remove a delegate from an empty list (or to remove a non-existent
delegate from a non-empty list) is not an error

---
WBR,
Michael  Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour 

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

I> Hi,
I>
I> "MikeP" <no_spam@_nospam.com> wrote in message
I> news:emLS7CF$HHA.3800@TK2MSFTNGP03.phx.gbl...
I>
>> Hi there,
>>
[quoted text clipped - 4 lines]
>> than once or is this implementation dependent (and possibly
>> dangerous). Thanks very much

I> I haven't read the documentation, but if they used the same pattern
I> than for the rest of the list I can tell you that it does not matter,
I> that it will removed if found and nothing happens if it does not.
I>
Michael Nemtsev, MVP - 21 Sep 2007 15:11 GMT
Hello Ignacio Machin ( .NET/ C# MVP )" machin TA laceupsolutions.com,

Yep, exactly.

C# Spec. 15.4
Attempting to remove a delegate from an empty list (or to remove a non-existent
delegate from a non-empty list) is not an error

---
WBR,
Michael  Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour 

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

I> Hi,
I>
I> "MikeP" <no_spam@_nospam.com> wrote in message
I> news:emLS7CF$HHA.3800@TK2MSFTNGP03.phx.gbl...
I>
>> Hi there,
>>
[quoted text clipped - 4 lines]
>> than once or is this implementation dependent (and possibly
>> dangerous). Thanks very much

I> I haven't read the documentation, but if they used the same pattern
I> than for the rest of the list I can tell you that it does not matter,
I> that it will removed if found and nothing happens if it does not.
I>
MikeP - 21 Sep 2007 15:56 GMT
> Hello Ignacio Machin ( .NET/ C# MVP )" machin TA laceupsolutions.com,
>
[quoted text clipped - 3 lines]
> Attempting to remove a delegate from an empty list (or to remove a
> non-existent delegate from a non-empty list) is not an error

Thanks very much to you and the others. It's strange however that the
section number you referred to (15.4) is actually 15.3 in the online version
of MSDN I'm now looking at. These section numbers don't correspond at all
however to the official downloaded spec from Sept/2006 but I think there may
be two versions floating around (for the ECMA and ISO perhaps). It's in
section 22.3 of the latter document anyway (on page 367 or 383 if you're
looking at the Adobe page number). The quote there however is:

"Attempting to subtract a delegate from null (or to subtract a non-existent
delegate from a non-empty list) is not an error".

In any case, I was really asking because sometimes you may want to assign
and remove an event in the course of processing a given function. If you
remove it in a "finally" clause however, and you jump through that clause
because of an exception, it's possible that you'll be removing the event
even though it was never assigned (because the exception occurred before
that took place). While you could organize things so this doesn't happen,
it's just not convenient sometimes, in particular when you're cleaning up
different resources (including other events possibly - the code can start to
become unwieldy if you introduce multiple "finally" and/or "catch" blocks if
you can get away with just one). Anyway, thanks again.
Peter Duniho - 21 Sep 2007 18:32 GMT
> [...]
> In any case, I was really asking because sometimes you may want to assign
> and remove an event in the course of processing a given function.

I agree that there are a number of situations in which removing without
knowing whether you've added would be a reasonable and useful design.

The one caveat I would stipulate is to take note that you can add the
same handler more than once.  I haven't personally seen a situation in
which this is necessarily a good design, but I'm not willing to say that
it could never happen.  In that case, removing a handler without knowing
specifically that it's been added could result in an incorrect number of
subscriptions by that handler to the event.

Pete
MikeP - 21 Sep 2007 19:56 GMT
>> In any case, I was really asking because sometimes you may want to assign
>> and remove an event in the course of processing a given function.
[quoted text clipped - 8 lines]
> specifically that it's been added could result in an incorrect number of
> subscriptions by that handler to the event.

Yes, I'm aware of that but it's not an issue in my case. Actually, this is
where I miss C++ (with no intention of starting a religious war). You can
simply rely on RAII to clean things up.
Nicholas Paldino [.NET/C# MVP] - 21 Sep 2007 15:12 GMT
Mike,

   As Ignacio says, the behavior is that nothing will happen.

   Furthermore, looking through the C# spec, I would say it is undefined
what the behavior is.

   However, I would strongly recommend that you keep track of this, as you
should know when event handlers need to be added and removed.  I would
almost classify it as a design bug when you don't (the same way I would
classify not knowing when your classes that implement IDisposable need to be
disposed of).

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi there,
>
[quoted text clipped - 3 lines]
> instance, can you safely apply "-=" more than once or is this
> implementation dependent (and possibly dangerous). Thanks very much
Nicholas Paldino [.NET/C# MVP] - 21 Sep 2007 15:36 GMT
Bleh, I didn't see section 15.4 which Michael pointed out.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Mike,
>
[quoted text clipped - 16 lines]
>> for instance, can you safely apply "-=" more than once or is this
>> implementation dependent (and possibly dangerous). Thanks very much
Som Nath Shukla - 22 Sep 2007 08:36 GMT
if  u are removing events more than once it will not show any
compiletime/runtime error it will work properly .
but variable will refenence to null.
show before calling uneed to check that event varaible is not null
others it will through exceptiin object refence not set

event delegatevaraible e;
e+= new  delegatevaraible (functionname);

e-= new  delegatevaraible (functionname);
e-= new  delegatevaraible (functionname);
// till now it willwork
// now u need to check
if(e!=null)
{
e.invoke(paramete)
}

> Hi there,
>
[quoted text clipped - 3 lines]
> instance, can you safely apply "-=" more than once or is this implementation
> dependent (and possibly dangerous). Thanks very much

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.