Why Invoke() can be called on .NET framework delegates and not on my own????

B

Bob Rock

Why doesn't this code compile???
Why can I call Invoke on .NET Framework defined delegates and not on
my own.

using System;

namespace DelegatesTest
{
public delegate void MyDelegate();

class MyClass
{
// .NET Framework defined delegates
public System.Threading.TimerCallback timerCallback = null;
public System.Timers.ElapsedEventHandler elapsedEventHandler = null;

// my own delegate
public DelegatesTest.MyDelegate myDelegate = null;

static void Main(string[] args)
{
MyClass myClass = new MyClass();

// OK
myClass.timerCallback.Invoke(null);
// OK
myClass.elapsedEventHandler.Invoke(null, null);
// NOT OK - will not compile
myClass.myDelegate.Invoke();
}
}
}

Don't look at the fact that the 3 delegates are not assigned any valid
method ... I tried writing the shortest possible example to explain my
problem.


Bob
 
D

Daniel O'Connell [C# MVP]

Bob Rock said:
Why doesn't this code compile???
Why can I call Invoke on .NET Framework defined delegates and not on
my own.

It appears to be a bug in the 1.x compilers. The 2.0 compiler errors out on
all three Delegate::Invoke calls.
 
W

William Stacey [MVP]

IIRC, to get it to work, just using "myDelegate();" to run the delegate.
 
B

Bob Rock

William Stacey said:
IIRC, to get it to work, just using "myDelegate();" to run the delegate.

William,

I know how to invoke a delegate, I was only wondering about the reason
for the behaviour I described in my post.


Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top