Hi All,

I started developing an application in WPF but I think the generic syntax for Relay Command and Delegate Command cannot be found very easily over the web and hence alogwith asking this question, I intend to start a thread for both Relay Command and Delegate Command.

So, I would request if anyone could please post the generic syntax for Relay Command and Delegate Command and also if possible please explain the differences between the two.

I intend to develop a generic syntax for all the methods (whether they have any return type or void, whether they have parameters passed or are parameter-less), so that anyone can easily find a generic syntax for the same and depending on the usage, can implement the method called through these commands.

Thanks a lot in advance :)

Recommended Answers

All 4 Replies

What do you mean by a generic syntax for it? ICommand and Delegate Command are pretty generic already. The intention is that you pass delegates or lambda functions in. As you already define what goes in/out of the method (by factor of delegates) I'm not sure what you stand to achieve.

What I meant to say here was there are several delegate commands syntax available for different purposes. Suppose you have one method as private void MyData() and you are calling it from a Delegate Command while on the other hand I have another method say, private object MyNewData(object obj) being called from the same Delegate Command as yours, so how do I write a generic Delegate Command which can be used for both the methods ?
I tried implementing the same but it allows only a single method either its not void or it is void or it may contain some parameters or not.
In other way round, I am not sure how to write such Delegate Command Class that would achieve a generic functionality for all kinds of methods.

I would question the fact your design requires you to call two different methods with, or without, a parameter.

Essentially in this case, you could keep your second definition and just handle the case for when obj == null

If you need to call two different methods (with different names) then that's simply a separate command. If you need to put two commands onto a single use case, then you need to go back to your design and see what's going on. If I ended up in that scenario, I would assume my design to be incorrect.

If you really want to go in your direction, then use a pattern whereby the delegate is merely a proxy to the method you really want to call.

(object obj) =>
{
    if(obj == null)
    {
        MakeCall();
        return null;
    }

    return MakeCall(obj);
}

Or you could try generic delegates (slightly clearner imo):

public delegate T CommandDelegate<T>(T inParam);

However, I'm going to question the design on this one. I really don't think something like this should be necessary.

I realise I'm criticising you, but I don't mean it in a nasty way.

I undersatnd the point you intend to mention over here.
Let me try the your suggestions and I will surely inform you as well over here :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.