Newby delegate problem - syntax problems I guess

C

Cosmin Prund

Hello everyone. I'm new to c# and I'm trying to use some delegates in my
code, so I can do time-tests on whatever I'm doing. In order to do this I
tried writing a testing method that will make use of a stopwatch and will
take as a parameter a delegate. I've seen this done by Nicholas Paldino in a
post and I tried to reproduce it (not copy-paste, but re-type and try to
understand).

So I start a new Console application project, I remove all the comments so I
can focus on my code, and I get what you'll see further down in this post.
Unfortunately the C# compiler doesn't like my code and I'm stuck on a VERY
basic concept:

(Q) How do I pass an delegate to a function that is supposed to accept one?

On my code I get this error:
"C:\...\Class1.cs(15): Method 'ConsoleApplication5.Class1.Dummy1()'
referenced without parentheses"

Here's my code:
using System;
namespace ConsoleApplication5
{
class Class1
{
public delegate void DummyTest();

public void Dummy1()
{}

public void DummyConsummer(DummyTest test)
{}

static void Main(string[] args)
{ DummyConsumer(Dummy1); } // Error line
}
}
 
T

Truong Hong Thi

Should be "new DummyTest(Dummy1)". Dummy1 should also be marked as
"static", or you have to instantiate Class1.
 
T

Truong Hong Thi

I forgot to mention that if you choose to mark as "static", you have to
mark DummyConsummer as "static" also.
 
R

Richard Blewett [DevelopMentor]

Cosmin Prund said:
Hello everyone. I'm new to c# and I'm trying to use some delegates in my
code, so I can do time-tests on whatever I'm doing. In order to do this I
tried writing a testing method that will make use of a stopwatch and will
take as a parameter a delegate. I've seen this done by Nicholas Paldino in
a post and I tried to reproduce it (not copy-paste, but re-type and try to
understand).

So I start a new Console application project, I remove all the comments so
I can focus on my code, and I get what you'll see further down in this
post. Unfortunately the C# compiler doesn't like my code and I'm stuck on
a VERY basic concept:

(Q) How do I pass an delegate to a function that is supposed to accept
one?

On my code I get this error:
"C:\...\Class1.cs(15): Method 'ConsoleApplication5.Class1.Dummy1()'
referenced without parentheses"

Here's my code:
using System;
namespace ConsoleApplication5
{
class Class1
{
public delegate void DummyTest();

public void Dummy1()
{}

public void DummyConsummer(DummyTest test)
{}

static void Main(string[] args)
{ DummyConsumer(Dummy1); } // Error line
}
}

The problem is you need to pass a delegate instance not a method to
DummyConsumer.

You are using version 1.1 of the framework so you need to do the following
in the DummyConsumer invocation

DummyConsumer(new DummyTest( Dummy1 ));

In version 2.0 your code would work. In version 2.0 C# introduces the
concept of delegate inference where the compiler works out what kind of
delegate creation to generate based on the methof signature.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
C

Cosmin Prund

Thanks Truong Hong Thi and Richard Blewett [DevelopMentor], that was it, I
had to instantiate an new delegate of the given type. Also I think the .NET
2.0 variant is much more intuitive.

Q: How is this handled in 2.0? Does the compiler instantiate the new
delegate automatically in the background or do delegates get a different
twist in 2.0?

Thanks again.
 
T

Truong Hong Thi

Q: How is this handled in 2.0? Does the compiler instantiate the new
delegate automatically in the background or do delegates get a different
twist in 2.0?
The compiler infers the delegate type from the method signature, and
instantiate it.
 

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