Article View: borland.public.csharpbuilder.language.csharp
Article #248Re: Passing by reference a bool argument to an event
From: Joanna Carter [TeamB]
Date: Sun, 06 Aug 2006 17:06
Date: Sun, 06 Aug 2006 17:06
60 lines
2380 bytes
2380 bytes
"Francois PIETTE [ICS - MidWare]" <francois.piette@overbyte.be> a οΏ½crit dans
le message de news: 44d5f1b5@newsgroups.borland.com...
|I need to pass by reference a boolean value to an event handler. Since
| passing arguments to event handler involve using an intermediate class
| deriving from EventArgs, the reference argument loose his reference aspect
| because bool is a value-type.
Here is a simple example that uses the CancelEventArgs doing something
similar.
public class Test
{
private CancelEventHandler validating;
public void OnValidating()
{
if (validating != null)
{
bool valid = false;
CancelEventArgs args = new CancelEventArgs(valid);
validating(this, args);
valid = args.Cancel;
Console.WriteLine(valid.ToString());
}
}
public event CancelEventHandler Validating
{
add { validating += value; }
remove { validating -= value; }
}
}
class Program
{
private static void HandleValidating(object sender, CancelEventArgs
args)
{
args.Cancel = true;
}
[STAThread]
static void Main(string[] args)
{
Test t = new Test();
t.Validating += HandleValidating;
t.OnValidating();
Console.ReadKey();
}
}
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
Technical Details
Message-ID:
<44d62288$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<44d5f1b5@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,a225bfd0924d364f 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: <44d5f1b5@newsgroups.borland.com> Subject: Re: Passing by reference a bool argument to an event Date: Sun, 6 Aug 2006 18:06:02 +0100 Lines: 62 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: <44d62288$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1154884232 82.69.239.102 (6 Aug 2006 10:10:32 -0700) X-Authenticated-User: jcarter Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail