πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.csharpbuilder.language.csharp
Article #48

Re: Custom Delegation

#48
From: 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 
Technical Details
Message-ID: <447abdd8$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <447aa0bd@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,bb4758d9cad65d98
X-Google-Attributes: gid1efe35,public
X-Google-Language: ENGLISH,ASCII
From: "Joanna Carter [TeamB]" <joanna@not.for.spam>
Newsgroups: borland.public.csharpbuilder.language.csharp
References: <447aa0bd@newsgroups.borland.com>
Subject: Re: Custom Delegation
Date: Mon, 29 May 2006 10:24:28 +0100
Lines: 55
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
NNTP-Posting-Host: 82.69.239.102
Message-ID: <447abdd8$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1148894680 82.69.239.102 (29 May 2006 02:24:40 -0700)
X-Authenticated-User: jcarter
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail