Robert
Robert

Reputation: 4406

How to turn these two methods into a delegate call?

I know that the following can be done with a delagate. What I don't know is how to do it. I have very little experience using delegates. I refactored out the specific stuff to each method and left the shared information in them.

public void AddCustomer(UserReportSavePermissionRequest permissionRequest)
{
    var key = AddGroupName(permissionRequest.GroupName);
    var reportKey = AddCustomerSpecific(permissionRequest, key);
    AddPermission(permissionRequest, reportKey);
}

public void AddOnline(UserReportSavePermissionRequest permissionRequest)
{
    var key = AddGroupName(permissionRequest.GroupName);
    var reportKey = AddOnlineSpecific(permissionRequest, key);
    AddPermission(permissionRequest, reportKey);
}

I am not sure how to make this happen. Also I am looking for a good tutorial on delegates to help me get a better understanding of them.

Upvotes: 1

Views: 73

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

Assuming that AssociateCustomerReportsToGroupNameBy and AssociateOnlineReportsToGroupNameBy have identical signatures, you could eliminate the repetition of the common code by passing in a delegate that has the same signature as these two methods, and calling it from the implementation. Rather than defining new delegates, you could use Func<UserReportSavePermissionRequest,string,R> and Action<T> types for it. TypeRcorresponds to the type ofreportKey. I am assuming thatkeyis of typestring, and thatReportGroupNameKeysreturnsstring[]`.

public void AddItem(
    UserReportSavePermissionRequest permissionRequest
,   Func<UserReportSavePermissionRequest,string,T> getReportKey
) {
    var key = AddGroupName(permissionRequest.GroupName);
    var reportKey = getReportKey(permissionRequest, key);
    AddPermission(permissionRequest, reportKey);
}

The caller needs to pass two additional parameters now. The call of AddItem that corresponds to your AddCustomer would look like this:

AddItem(permissionRequest, AddCustomerSpecific);

The call of AddOnline would be

AddItem(permissionRequest, AddOnlineSpecific);

Upvotes: 2

AlwaysAProgrammer
AlwaysAProgrammer

Reputation: 2919

Declare a delegate as class level variable

public delegate void MyDelegate(UserReportSavePermissionRequest)

Instantiate the delegate

MyDelegate del = new MyDelegate(AddCustomer);
MyDelegate del2 = new MyDelegate(AddOnline);

Alternatively, if you can even chain the delegates.

invoke the method on the delegate by passing a UserReportSavePermissionRequest instance

del(permissionRequest);

.Net provides in built delegates. They are Func, Action and Predicate A Func delegate represents a method that takes parameters and returns something An Action delegate represents a method that takes parameters and returns nothing A predicate returns a bool

Note that the parameters to the methods are optional and not always required In you case, the Action delegate can be used if wish to pass the methods as parameter to a method

e.g

public void MyMethod(Action<UserReportSavePermissionRequest> act, UserReportSavePermissionRequest obj)
{
    act.Invoke(obj);
}

The above method can be called as

UserReportSavePermissionRequest obj;

MyMethod(AddCustomer, obj);
MyMethod(AddOnline, obj)

Upvotes: 1

T McKeown
T McKeown

Reputation: 12847

You don't give much explanation as to why you want these to be delegates so I will assume you want to be able to call these methods from other classes.

Create a delegate type that matches the signature of your method(s):

Your signature for your delegates is one with no return value and 1 parameter of type UserReportSavePermissionRequest. So add this delegate as a public delegate or in a separate .cs file in Visual Studio

public delegate void UserReportSavePermissionRequestDelegate(UserReportSavePermissionRequest);

Now lets use it to call one of your 2 methods that implement this signature:

//some code where you want to pass in a delegate so the delegated method can be called:
someObject.Register( this.AddCustomer);  // <-- you are passing a method that matches the delegate signature

The someObject.Register() method would be defined like this:

public void Register(UserReportSavePermissionRequestDelegate del)
{
   ... //do some stuff
   var someVar  = CreateRequest(); // <-- this needs to be the UserReportSavePermissionRequest type
   del(someVar);
}

Upvotes: 2

Related Questions