When I try to compile the following:
public static delegate void MoveDelegate (Actor sender, MoveDirection args);
I receive, as an error: "The modifer 'static' is not valid for the this item."
I'm implementing this within a singleton, with a separate class which calls the delegate. The problem is that when I use the singleton instance within the other class to call the delegate (from the identifier, not the type), I can't do that for whatever reason, even when I declare the delegate non-static. Obviously, I can only refer to it via the type directly if and only if the delegate is static.
What is the reasoning behind this? I am using MonoDevelop 2.4.2.
update
After trying one of the suggestions with the following code:
public void Move(MoveDirection moveDir)
{
ProcessMove(moveDir);
}
public void ProcessMove(MoveDirection moveDir)
{
Teleporter.MoveMethod mm = new Teleporter.MoveMethod(Move);
moveDelegate(this, moveDir);
}
I've received a processing error, which states that the MoveMethod must be a type, and not an identifier.
A delegate declaration basically declares a method signature, which only includes information about its parameters and return type. And since the same delegate can point to both static and instance methods, it doesn't make sense to make the method signature itself static or instance.
Once you have declared your delegate as:
it means that any delegate of this type must point to a method which accepts one
Actor
parameter, oneMoveDirection
parameter, and returnsvoid
, regardless of whether the method is static or instance. You can declare the delegate at namespace scope, or inside a class (just like you would declare a nested class).So after declaring the
MoveDelegate
somewhere, you can the create fields and variables of that type:and remember that the method should have a matching signature:
then you can assign this method to a delegate at some other place:
It is useful to know that .NET (starting from version v3.5) provides some predefined generic delegates (
Action
andFunc
) which can be used instead of declaring your own delegates:Using those delegates is IMHO more readable, since you can immediately identify parameters' signature from looking at the delegate itself (while in your case one needs to look for the declaration).
You are declaring a
delegate
type. It doesn't make any sense to declare it asstatic
. You could declare an instance of yourdelegate
type asstatic
, though.Delegate declaration is actually a type declaration. It cannot be static, just like you cannot define a static enum or structure.
However, I would rather use an interface instead of raw delegate.
Consider this:
In your calling code:
This similar in spirit to Strategy design pattern and allows you to decouple
Actor
movement from the actual implementation strategy (console, graphic, whatever). Other strategy methods may later be required which makes it a better choice than a delegate.Finally, you can use an Inversion of Control framework to automatically inject correct strategy instance in your
Actor
classes so there is no need for manual initialization.define your delegate, in your static class declare an instance variable for it.
Let me tell you what happened when you declared a delegate
The compiler creates a class, in this case named
MoveDelegate
, and extends it withSystem.MulticastDelegate
.Since you can not extend any non static type by static type.
So this is the reason why the compiler does not allow static delegate declaration. But still you can have static delegate reference.
Try this:
So the method-variable can be defined static. The keyword
static
has no meaning for thedelegate
definition , just likeenum
orconst
definitions.An example of how to assign the static method-field: