473,386 Members | 1,827 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.

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 29782
"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.com 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.com 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(SomeMethod); // 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.WriteLine("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
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....
1
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...
1
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...
3
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...
2
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...
4
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...
0
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...
4
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...
3
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
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
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...

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.