473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Newby delegate problem - syntax problems I guess

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
}
}

Nov 17 '05 #1
5 1084
Should be "new DummyTest(Dummy1)". Dummy1 should also be marked as
"static", or you have to instantiate Class1.

Nov 17 '05 #2
"Cosmin Prund" <co**********@adicomsft.NOSPAM.ro> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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
Nov 17 '05 #3
I forgot to mention that if you choose to mark as "static", you have to
mark DummyConsummer as "static" also.

Nov 17 '05 #4
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.
Nov 17 '05 #5
>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.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: WindAndWaves | last post by:
Hi Gurus I am keen to make a search page on a website, but I have absolutely zero experience with PHP. I am going to hire an expert, but I thought that it may pay to try it a bit first myself...
9
by: Damien | last post by:
I have just built a simple stopwatch application, but when i f5 to get things goings i get this message, An unhandled exception of type 'System.ArithmeticException' occurred in...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
11
by: ohmmega | last post by:
hello world. i would like to implement a class with a timer, witch informs me every second about it's tick. the code works already, but i would like to change a thing (or more). <code> //at...
6
by: damiensawyer | last post by:
Hi, Can someone please explain to me something about delegates? My understanding is as follows. A delegate is basically an object that can hold a reference to a "method" somewhere. That is,...
7
by: raylopez99 | last post by:
On Aug 16, 3:49 pm, Marc Gravell <marc.grav...@gmail.comwrote: If you cannot understand such a simple post, then you don't understand generic delegate types. Apparently you can only read your...
7
by: Stefan Hoffmann | last post by:
hi @all, is there something like an anonymous delegate? This is the original code: -- private delegate void DelegateSetFormCaption(string text); private void SetFormCaption(string text) {
10
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.