No AddressOf in C#

  • Thread starter Thread starter ECVerify.com
  • Start date Start date
E

ECVerify.com

I am trying to figure out how to do this in C Sharp

In VB this works perfect
MenuItem2.MenuItems.Add("123", AddressOf Me.MenuItem1_Click)


The problem is there is no address of in C#, I tried this in C#
menuItem2.MenuItems.Add("123", MenuItem1_Click);

But it does not seem to work, since C# has no AddressOf how can I acomplish this?

Thanks, Ed,
 
Hi,

That would be:
menuItem2.MenuItems.Add("123", new EventHandler(MenuItem1_Click));
 
AddressOf in vb.net actually uses the compiler to infer the type of a
delegate that is required, then turns your :
AddressOf(MenuItem1_Click)
into
new EventHandler(MenuItem1_Click)
or, if it's another delegate type needed:
new MyDelegateType(MenuItem1_Click)

In c#, you need to create the delegate yourself and pass it. So turn
"AddressOf(xxx)" into "new EventHandler(xxx)"
 
Miha Markic said:
Hi,

That would be:
menuItem2.MenuItems.Add("123", new EventHandler(MenuItem1_Click));


--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

ECVerify.com said:
I am trying to figure out how to do this in C Sharp

In VB this works perfect
MenuItem2.MenuItems.Add("123", AddressOf Me.MenuItem1_Click)


The problem is there is no address of in C#, I tried this in C#
menuItem2.MenuItems.Add("123", MenuItem1_Click);

But it does not seem to work, since C# has no AddressOf how can I acomplish this?

Thanks, Ed,

Thanks Everyone
 
Philip Rieck said:
AddressOf in vb.net actually uses the compiler to infer the type of a
delegate that is required, then turns your :
AddressOf(MenuItem1_Click)
into
new EventHandler(MenuItem1_Click)
or, if it's another delegate type needed:
new MyDelegateType(MenuItem1_Click)

In c#, you need to create the delegate yourself and pass it. So turn
"AddressOf(xxx)" into "new EventHandler(xxx)"

Thanks Everyone
 
It should be noted that the 2.0 version of C# is supposed to provide
automatic delegate inference, using syntax like event += Method; however I
don't know how or if it will work, I'm basing this on PDC slides.
 
Daniel O'Connell said:
It should be noted that the 2.0 version of C# is supposed to provide
automatic delegate inference, using syntax like event += Method; however I
don't know how or if it will work, I'm basing this on PDC slides.


Daniel,

The C# compiler allows you to specify the name of the callback method
without having to construct a delegate object wrapper, because he is capable
of inferring this on its own, what you get is just a syntactic shortcut.

Consider following sample:

class MyClass {

ThreadPool.QueueWorkItem(MyAsynchMethod, 123);

....



static void MyAsynchMethod(object obj) {

...

}

Here, the Threadpool class's QueueWorkItem expects a WaitCallback delegate
reference that contains a reference to a method as first arg. The C#
compiler is capable of inferring the missing WaitCallback delegate
construction, by producing the IL that does create an instance of the
WaitCallback delegate object.

Willy.
 
Back
Top