PC Review


Reply
Thread Tools Rate Thread

anonymous method

 
 
Tony Johansson
Guest
Posts: n/a
 
      16th Mar 2010
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


 
Reply With Quote
 
 
 
 
Family Tree Mike
Guest
Posts: n/a
 
      16th Mar 2010
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
 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      16th Mar 2010
On 2010-03-16, Family Tree Mike <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: anonymous method help Martin Honnen Microsoft C# .NET 2 6th Mar 2009 06:55 PM
Re: anonymous method help dunawayc@gmail.com Microsoft C# .NET 0 6th Mar 2009 02:23 PM
Anonymous Method vs. Anonymous Delegate Robert Howells Microsoft C# .NET 3 21st Oct 2007 08:08 AM
Anonymous Method Bob Microsoft C# .NET 2 8th Oct 2007 01:54 PM
Anonymous method Maury Microsoft Dot NET Framework 2 20th Mar 2007 03:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:57 AM.