473,787 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 'ConsoleApplica tion5.Class1.Du mmy1()'
referenced without parentheses"

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

public void Dummy1()
{}

public void DummyConsummer( DummyTest test)
{}

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

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

Nov 17 '05 #2
"Cosmin Prund" <co**********@a dicomsft.NOSPAM .ro> wrote in message
news:%2******** ********@TK2MSF TNGP09.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 'ConsoleApplica tion5.Class1.Du mmy1()'
referenced without parentheses"

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

public void Dummy1()
{}

public void DummyConsummer( DummyTest test)
{}

static void Main(string[] args)
{ DummyConsumer(D ummy1); } // 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(n ew 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
2189
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 (I do want to learn how to do it). What I have is a search page (HTML) and an access database. Now, can someone give me some clues or links on where to start. I prefer not to install the whole PHP thing on my own computer, but just to
9
2608
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 system.drawing.dll Additional information: Overflow or underflow in the arithmetic operation.
29
2477
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" for when and how to use single quotes and double quotes in ASP? thanks! ---------------------- SQL = SQL & "WHERE '" & REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE("GenKTitles.
11
2352
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 method dynamically to match the arguments supplied to it when it is defined. For example... public delegate void MyDelegate(string myString, int myInt);
11
1832
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 the moment i initialize the object with the constructor: public myClass(System.Timers.ElapsedEventHandler CallMeBack) {
6
1405
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, it's essentially a pointer to a piece of code somewhere else in memory. It therefore (to me anyway) makes sense to define delegates with their signatures and be able to use those signatures at different points in
7
1684
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 own code, which makes you a beginner, like me, perhaps worse. But 'thanks' for your time anyway. RL
7
2726
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
2135
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. Let's say we have 2 delegates: public delegate void FirstDelegate( int i ); public delegate void SecondDelegate( int i );
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10169
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.