473,671 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot put delegate in interface - why not?

When I try and place a delegate in an interface, like this :

public interface ITest {
double foo();
delegate void bar();
}

I get an error "delegates cannot declare types". What is it about a
delegate that makes it incompatible in an interface. I know that an
interface cannot have any implementation - is there some implementation
detail in a delegate that I don't know about?

Jun 29 '06 #1
5 29795
"delegate void bar();"

is actually a type declaration of the "bar" type, and *not* an instance
member. Perhaps you mean:

delegate void MyDelegate();
public interface ITest {
double foo();
MyDelegate bar;
}

Marc
Jun 29 '06 #2
at*****@yahoo.c om wrote:
When I try and place a delegate in an interface, like this :

public interface ITest {
double foo();
delegate void bar();
}

I get an error "delegates cannot declare types". What is it about a
delegate that makes it incompatible in an interface. I know that an
interface cannot have any implementation - is there some implementation
detail in a delegate that I don't know about?


You've tried to do something like:

public interface ITest
{
public class Foo
{
}
}

Declaring a delegate is declaring a type, which can't be part of an
interface. You should declare the delegate itself elsewhere - you can
have a member of the type which *uses* the delegate, but that's a
different matter.

Jon

Jun 29 '06 #3
Jon Skeet [C# MVP] wrote:
at*****@yahoo.c om wrote:
When I try and place a delegate in an interface, like this :

public interface ITest {
double foo();
delegate void bar();
}

I get an error "delegates cannot declare types". What is it about a
delegate that makes it incompatible in an interface. I know that an
interface cannot have any implementation - is there some implementation
detail in a delegate that I don't know about?


You've tried to do something like:

public interface ITest
{
public class Foo
{
}
}

Declaring a delegate is declaring a type, which can't be part of an
interface. You should declare the delegate itself elsewhere - you can
have a member of the type which *uses* the delegate, but that's a
different matter.

Jon


Thanks for your replies - I think my question is : is the delgate a
type because it inherits from System.Delgates , even though the delegate
itself is not instantiated? I always thought of a delegate as a
"function pointer", but it's also a class and therefore a type.

Jun 29 '06 #4
An ***instance*** of a delegate is (essentially) a function pointer;
however, starting with the word "delegate" (with a small d) here defines the
***type*** - which is primarily the method signature for the Invoke method -
i.e. in this case it takes nothing and returns nothing. If it was a large D
(Delegate) then it would be any instance. Confusing, eh?

I've amended my previous code (to compile; whoops), and added some examples:

public delegate void MyDelegate();
public interface ITest {
double foo();
MyDelegate bar {get; set;}
Delegate fred {get; set;}
}
//...
ITest somet = new TestClasss(); // that implements ITest
somet.bar = new MyDelegate(Some Method); // creates an instance of MyDelegate
with SomeMethod as the target
somet.fred = // pretty much any random delegate, but must invoke via
DynamicInvoke
//...
static void SomeMethod() {}

Note: to add confusion, in the RHS of 2.0 expressions, the word "delegate"
can signify an /anonymous/ delegate, which /is/ an instance, not a type -
e.g.

button.Click += delegate {Debug.WriteLin e("Hi");};

Marc
Jun 29 '06 #5
Hi,


Thanks for your replies - I think my question is : is the delgate a
type because it inherits from System.Delgates ,
Yes, of course.
even though the delegate
itself is not instantiated?
The example of Jon does not instantiate nothing, just declare a type inside
an interface, which is the error you are getting
I always thought of a delegate as a
"function pointer", but it's also a class and therefore a type.


In .NET all definitions are types. from enum to attributes to classes.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jun 29 '06 #6

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

Similar topics

0
9542
by: Yannick Turgeon | last post by:
Hello all, Back-end: SS2000 SP2 + NT4 Front-end: A97 + XP We are currently trying to add a Domain Server to our network. We added our SQL Server to the domain and added a client too. Everything is working fine... for maybe 20 or 30 minutes. Then the client generates this error: -----------------
1
404
by: serge calderara | last post by:
Dear all, I have a VB.NET application which is able to identify and load dynamically other dll's. created for my application. Those dll's are working as plugins for my main application. I need to used Delegate type for my plugins and then rehister to that delegate from my main application. I delcare my DElegate type as follow :
1
4781
by: Nadav | last post by:
Hi, I Wonder... Can a server-side class exposed through remoting expose a delegate? In other words: can a client set a server-side delegate that in-turn will be asynchronously called from the server on the client? Thanks, Nadav.
3
8877
by: Phill | last post by:
When should you use an interface over a delegate? Like if you have a class that needs to sort various types of objects. Should it use a delegate to do the sorting or require that the objects implement a sorting interface? I read that delegates are smaller and of finer granularity than interfaces. What does that mean exactly? Does it mean they are more efficient?
2
1733
by: Ruediger Klaehn | last post by:
I have written an arithmetic expression compiler. Now that it is finished I would like to give it a nice interface. Currently I have this interface: delegate double Function(double x); class ArithmeticExpression { public static Function Compile(string text); ... }
4
1866
by: pz0071 | last post by:
G'day, I think I got my head around using delegates, particularily when updating the user interface from another thread. I got it all sussed, just not 100% sure on the correct syntax for calling the delegate. Is this.Invoke(delUpdateUI, args); Same as delUpdateUI(i);
0
2495
by: Wavemaker | last post by:
One of the things I've struggled with from time to time is handling events raised on different threads. For example, an object could be listening to events from one or more objects running in different threads besides itself. There is a concern for thread safety. There are various ways of dealing with this. One way is to make the class thread safe by putting in locks. But another way is to stream the events from the various threads into a...
4
1572
by: Steven Cummings | last post by:
Hello, I'm currently developing on a large project where several assemblies build into DLLs that support the final .NET EXE. The original plan was to implement the project in VB.NET, so the EXE project is a VB WinForms App. Later on we got approval to not stick to that requirement so I started writing library code in C# (which I much prefer). Of interest in my problem are two C# DLL projects aside from the main EXE project. The first,...
3
11551
by: steve | last post by:
Hi all, I want to be able to hirearchally define event delegate (declarations) WITHIN interfaces. Unfortunatelly C# 1 doesn't appear to support it. How does the following look as a work-around? Are there better ways of doing this?
0
8912
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
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...
1
8597
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8669
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
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...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
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();...
2
2049
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1807
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.