views:

109

answers:

3

I need to have some delegates in my class.

I'd like to use the interface to "remind" me to set these delegates.

How to?

my class look like this

public class ClsPictures : myInterface
{       
    // Implementing the IProcess interface
    public event UpdateStatusEventHandler UpdateStatusText;
    public delegate void UpdateStatusEventHandler(string Status);

    public event StartedEventHandler Started;
    public delegate void StartedEventHandler();
}

I need an interface to force those delegates:

public interface myInterface
{
   // ?????       
}
+7  A: 

Those are declaring delegate types. They don't belong in an interface. The events using those delegate types are fine to be in the interface though:

public delegate void UpdateStatusEventHandler(string status);
public delegate void StartedEventHandler();

public interface IMyInterface
{       
    event UpdateStatusEventHandler StatusUpdated;    
    event StartedEventHandler Started;
}

The implementation won't (and shouldn't) redeclare the delegate type, any more than it would redeclare any other type used in an interface.

Jon Skeet
@Skeet don't you ever have a day off?!? ;)
w69rdy
A: 

use abstract class instead of interface, also delegate definition doesn't need any class definition.

SaeedAlg
I don't see why you'd suggest using an abstract class. An interface is fine here.
Jon Skeet
u used event may be someone want make his delegate definition in his class, not using any event, (for example invoke methods async). its not necessary define in interface or class but for redemption is good. at last, i didn't see what u wrote i post this, after that i see ur answer, I'm not in conjunction with ur idea.
SaeedAlg
@SaeedAlg: I'm afraid I don't understand you. But if Asaf wants to express the concept of "you can be notified when this has started or the status has been updated" then events in an interface are entirely appropriate.
Jon Skeet
see http://stackoverflow.com/questions/3949858/for-delegates-witch-one-is-better-and-when-how-do-u-think
SaeedAlg
Jon, may be u wrote some book and u have 500K in sof but its not dictate that u are God father of c# :D
SaeedAlg
@saeed yes it does. Wait, I can't believe I just said that.
Will
+3  A: 

Jon Skeet's answer is right, I just want to add a note.

Interfaces are not there for "reminding" you what to do, or what to include in your classes. Interfaces are means of abstraction, used in Object Oriented programming and design methods. Maybe you don't need an interface declaration at all, unless you want to see some concrete class instances as the interface elsewhere in your program (Abstraction).

If you want to enforce some coding standards in your project, you may want to try using code analysis tools (like in Visual Studio) - They allow extensions, that you can incorporate to add you own code analysis rules.

Using code analysis, if you "forget" to add the delegates (although I don't see the point of forgetting it, as if the delegate is not used, it's not needed) you'll get a warning / error.

Iravanchi
You may be right, but there are some times when you make some assumptions that even with good documentaion it's hard to remember after a while. I try to reuse my code but sometimes I can't remember what it the core and what it not (when it comes to delegates - it's hard for me to see the big picture...)
Asaf