🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.csharpbuilder.language.csharp
7 messages
7 total messages Started by Andre Mon, 29 May 2006 07:20
Custom Delegation
#43
Author: Andre
Date: Mon, 29 May 2006 07:20
28 lines
1823 bytes
I have a series of my own eventHandler delegates, and a few events using 
these delegates. All works fine. Im just concerned about one thing.
In some litrature the author define a event like this:

public event eventHandler onEvent
{
    add{/// some code}
    remove{/// some code}
}

assuming which are used when doing:

myObj.onEvent += new eventHandler(this.onEventAction);

and

myObj.onEvent -= new eventHandler(this.onEventAction);

Now, is this a cleanup routine when using -= or does it just add additional 
functionality to the event contruction/destruction?
Is this type of coding neccesary? good practice or mandatory for event 
cleanup?
As far as I know, the object owning the event gets disposed by GC 
automaticlly.
Should I call the -= manouver when the object gets disposed?

Thanks,
Andre 
Re: Custom Delegation
#48
Author: Joanna Carter [TeamB]
Date: Mon, 29 May 2006 09:24
53 lines
2644 bytes
"Andre" <andretruter@webmail.co.za> a �crit dans le message de news: 
447aa0bd@newsgroups.borland.com...

|I have a series of my own eventHandler delegates, and a few events using
| these delegates. All works fine. Im just concerned about one thing.
| In some litrature the author define a event like this:
|
| public event eventHandler onEvent
| {
|    add{/// some code}
|    remove{/// some code}
| }
|
| assuming which are used when doing:
|
| myObj.onEvent += new eventHandler(this.onEventAction);
|
| and
|
| myObj.onEvent -= new eventHandler(this.onEventAction);
|
| Now, is this a cleanup routine when using -= or does it just add 
additional
| functionality to the event contruction/destruction?

Using -= removes a handler from an event, nothing more.

| Is this type of coding neccesary? good practice or mandatory for event
| cleanup?

This style of coding where you have to call "+/-= new EventHandler(...)" 
become optional in C# 2.0 where you can simplify the statement to something 
like :

myObj.Event += HandleEventAction;
myObj.Event -= HandleEventAction;

| As far as I know, the object owning the event gets disposed by GC
| automaticlly.
| Should I call the -= manouver when the object gets disposed?

No, you don't need to do anything as you don't know when the objects will 
actually get collected. The only possible gotcha is if you create temporary 
objects with handler methods; if you were to expect the temporary objects to 
be GCed, you would have to ensure that their handler methods were 
disconnected, otherwise the list of delegates would grow indefinitely 
everytime a new temporary object's handler was connected.

Joanna

-- 
Joanna Carter [TeamB]
Consultant Software Engineer 
Re: Custom Delegation
#47
Author: Andre
Date: Mon, 29 May 2006 09:42
62 lines
3050 bytes
thanks for clearing that out for me.

"Joanna Carter [TeamB]" <joanna@not.for.spam> wrote in message 
news:447abdd8$1@newsgroups.borland.com...
> "Andre" <andretruter@webmail.co.za> a �crit dans le message de news:
> 447aa0bd@newsgroups.borland.com...
>
> |I have a series of my own eventHandler delegates, and a few events using
> | these delegates. All works fine. Im just concerned about one thing.
> | In some litrature the author define a event like this:
> |
> | public event eventHandler onEvent
> | {
> |    add{/// some code}
> |    remove{/// some code}
> | }
> |
> | assuming which are used when doing:
> |
> | myObj.onEvent += new eventHandler(this.onEventAction);
> |
> | and
> |
> | myObj.onEvent -= new eventHandler(this.onEventAction);
> |
> | Now, is this a cleanup routine when using -= or does it just add
> additional
> | functionality to the event contruction/destruction?
>
> Using -= removes a handler from an event, nothing more.
>
> | Is this type of coding neccesary? good practice or mandatory for event
> | cleanup?
>
> This style of coding where you have to call "+/-= new EventHandler(...)"
> become optional in C# 2.0 where you can simplify the statement to 
> something
> like :
>
> myObj.Event += HandleEventAction;
> myObj.Event -= HandleEventAction;
>
> | As far as I know, the object owning the event gets disposed by GC
> | automaticlly.
> | Should I call the -= manouver when the object gets disposed?
>
> No, you don't need to do anything as you don't know when the objects will
> actually get collected. The only possible gotcha is if you create 
> temporary
> objects with handler methods; if you were to expect the temporary objects 
> to
> be GCed, you would have to ensure that their handler methods were
> disconnected, otherwise the list of delegates would grow indefinitely
> everytime a new temporary object's handler was connected.
>
> Joanna
>
> -- 
> Joanna Carter [TeamB]
> Consultant Software Engineer
>
> 
Re: Custom Delegation
#49
Author: Marc Rohloff TeamB
Date: Mon, 29 May 2006 13:28
17 lines
1796 bytes
On Mon, 29 May 2006 09:20:28 +0200, Andre wrote:

> Now, is this a cleanup routine when using -= or does it just add additional 
> functionality to the event contruction/destruction?
> Is this type of coding neccesary? good practice or mandatory for event 
> cleanup?
> As far as I know, the object owning the event gets disposed by GC 
> automaticlly.
> Should I call the -= manouver when the object gets disposed?

What Joanna said is true but since you showed a custom implementation
almost anything could happen.
The best practice is probably to always remove the events.

-- 
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com
Re: Custom Delegation
#50
Author: Andre
Date: Mon, 29 May 2006 14:04
38 lines
2246 bytes
> almost anything could happen.
> The best practice is probably to always remove the events.

by this you mean that i should define special 'remove' for my event like
{
    public event eventHandler onEvent
    {
        remove {//// some cleanup code }
    }

and then use -= in object dispose.
}
or
{
    just use the -= in object dispose.
}


"Marc Rohloff [TeamB]" <"on request"> wrote in message 
news:15xbx0js9z62p.dlg@dlg.marcrohloff.com...
> On Mon, 29 May 2006 09:20:28 +0200, Andre wrote:
>
>> Now, is this a cleanup routine when using -= or does it just add 
>> additional
>> functionality to the event contruction/destruction?
>> Is this type of coding neccesary? good practice or mandatory for event
>> cleanup?
>> As far as I know, the object owning the event gets disposed by GC
>> automaticlly.
>> Should I call the -= manouver when the object gets disposed?
>
> What Joanna said is true but since you showed a custom implementation
> almost anything could happen.
> The best practice is probably to always remove the events.
>
> -- 
> Marc Rohloff [TeamB]
> marc rohloff -at- myrealbox -dot- com 
Re: Custom Delegation
#201
Author: Marc Rohloff TeamB
Date: Tue, 30 May 2006 00:59
29 lines
1979 bytes
On Mon, 29 May 2006 16:04:49 +0200, Andre wrote:

>> almost anything could happen.
>> The best practice is probably to always remove the events.
> 
> by this you mean that i should define special 'remove' for my event like
> {
>     public event eventHandler onEvent
>     {
>         remove {//// some cleanup code }
>     }
> 
> and then use -= in object dispose.
> }
> or
> {
>     just use the -= in object dispose.
> }

I am not sure where you are going.
What I meant is that if you subscribe for an event then you should
unsubscribe when you are finished or when you are disposed.

If you are writing event properties you should probably stick with the
default implementation.

-- 
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com
Re: Custom Delegation
#202
Author: Andre
Date: Tue, 30 May 2006 07:40
34 lines
2143 bytes
Default implementation it is. Thanks for the help.

"Marc Rohloff [TeamB]" <"on request"> wrote in message 
news:1ol94ym2046bi$.dlg@dlg.marcrohloff.com...
> On Mon, 29 May 2006 16:04:49 +0200, Andre wrote:
>
>>> almost anything could happen.
>>> The best practice is probably to always remove the events.
>>
>> by this you mean that i should define special 'remove' for my event like
>> {
>>     public event eventHandler onEvent
>>     {
>>         remove {//// some cleanup code }
>>     }
>>
>> and then use -= in object dispose.
>> }
>> or
>> {
>>     just use the -= in object dispose.
>> }
>
> I am not sure where you are going.
> What I meant is that if you subscribe for an event then you should
> unsubscribe when you are finished or when you are disposed.
>
> If you are writing event properties you should probably stick with the
> default implementation.
>
> -- 
> Marc Rohloff [TeamB]
> marc rohloff -at- myrealbox -dot- com 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads