From: Tony Johansson on
Hi!

Here is a simple example on an anonymous method that works
test.myEvent += delegate(object sender, EventArgs e)
{
Console.WriteLine("Test");
};

How do I write the anonymous method within the EventHandler I have tried
here but this gives compile error
test.myEvent +=new EventHandler(Console.WriteLine("Test"););

//Tony


From: Family Tree Mike on
On 3/16/2010 4:11 PM, Tony Johansson wrote:
> Hi!
>
> Here is a simple example on an anonymous method that works
> test.myEvent += delegate(object sender, EventArgs e)
> {
> Console.WriteLine("Test");
> };
>
> How do I write the anonymous method within the EventHandler I have tried
> here but this gives compile error
> test.myEvent +=new EventHandler(Console.WriteLine("Test"););
>
> //Tony
>
>

I prefer your first example, but if you want, you could do this:

test.myEvent += new
EventHandler(delegate {Console.WriteLine("Test");});


--
Mike
From: Tom Shelton on
On 2010-03-16, Family Tree Mike <FamilyTreeMike(a)ThisOldHouse.com> wrote:
> On 3/16/2010 4:11 PM, Tony Johansson wrote:
>> Hi!
>>
>> Here is a simple example on an anonymous method that works
>> test.myEvent += delegate(object sender, EventArgs e)
>> {
>> Console.WriteLine("Test");
>> };
>>
>> How do I write the anonymous method within the EventHandler I have tried
>> here but this gives compile error
>> test.myEvent +=new EventHandler(Console.WriteLine("Test"););
>>
>> //Tony
>>
>>
>
> I prefer your first example, but if you want, you could do this:
>
> test.myEvent += new
> EventHandler(delegate {Console.WriteLine("Test");});
>
>

You don't even have to do that in 2008:

test.myEvent += (o, ea) => Console.WriteLine("Test");

--
Tom Shelton