Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Is there any advantige using delegates instead of methods? For example:

public void LoginAuthenticate(object sender, AuthenticateEventArgs e)
{
 if (Membership.ValidateUser(lgUserLogin.UserName, lgUserLogin.Password))
     {
       Func<string, string> getDestinationPage = (userLogin) => { //definition };

     e.Authenticated = true;
     lgUserLogin.DestinationPageUrl = getDestinationPage(lgUserLogin.UserName);
  }

else
   {
    Action<string> invalidLogin = (msg) =>
     {
       lgUserLogin.FailureText = string.IsNullOrWhiteSpace(msg) ? "Invalid login" : msg;
       e.Authenticated = false;
     };

     MembershipUser user = Membership.GetUser(lgUserLogin.UserName);
      if (user != null)
       {
        if (user.IsLockedOut)
         {
           invalidLogin("User has been blocked");

         }
         else if (!user.IsApproved)
          {
           invalidLogin("User is not activated");
          }
          else
           {  
             invalidLogin(null);  
           }

       }

       else
         {
           invalidLogin(null);
         }
   }
}

If I getDestinationPage define as private string getDestinationPage(string login){...} and invalidLogin as private void invalidLogin(string msg){...}

does it gives any benefits?

share|improve this question

migrated from stackoverflow.com Jul 21 '11 at 3:51

This question came from our site for professional and enthusiast programmers.

5 Answers 5

up vote 3 down vote accepted

In your case it makes the code cleaner and more readable to use methods. There is no benefit in using delegates like this.

Your code would look like this:

private string getDestinationPage(string userLogin)
{
    // definition
}

private void invalidLogin(string msg)
{
    lgUserLogin.FailureText = string.IsNullOrWhiteSpace(msg)
                              ? "Invalid login"
                              : msg;
}

public void LoginAuthenticate(object sender, AuthenticateEventArgs e)
{
    if (Membership.ValidateUser(lgUserLogin.UserName, lgUserLogin.Password))
    {
        e.Authenticated = true;
        lgUserLogin.DestinationPageUrl = getDestinationPage(lgUserLogin.
                                                              UserName);
    }

    else
    {
        e.Authenticated = false;
        MembershipUser user = Membership.GetUser(lgUserLogin.UserName);
        if (user != null)
        {
            if (user.IsLockedOut)
            {
                invalidLogin("User has been blocked");
            }
            else if (!user.IsApproved)
            {
                invalidLogin("User is not activated");
            }
            else
            {
                invalidLogin(null);
            }
        }
        else
        {
            invalidLogin(null);
        }
    }
}
share|improve this answer
    
But you are missing the fact that it is not just a function pointer, but in fact a closure with some free variables. You cannot do that with methods alone. You need a classes too. –  leppie Jul 20 '11 at 9:54
    
See stackoverflow.com/questions/2497801/… for an explanation :) –  leppie Jul 20 '11 at 9:55
    
@leppie: The only variable he uses is e as far as I can see. This could easily be passed to the private method. No need for a class in his case. Thanks for the link, but I know about closures and the classes that are created in the background. –  Daniel Hilgarth Jul 20 '11 at 9:55
2  
@leppie: In his code, it isn't even necessary to access e in the method. –  Daniel Hilgarth Jul 20 '11 at 10:08
2  
My code is cleaner and more readable, yes. The reason is that the definition of invalidLogin and getDestinationPage is not cluttered inside the other code. –  Daniel Hilgarth Jul 20 '11 at 10:22

If you are going to work with different scenarious where users may be redirected to different destination pages (programatically), and there are several different methods handling this depending on the situation with some conditionals etc., then yes delegates have the advantage as you may change the method to call just by assigning the new method to the delegate instance. Other than that, I don't see any point.

share|improve this answer

Lambdas are anonymous methods. You use them mostly in cases where you want to pass a bit of code into a method as a parameter.

For example all LINQ methods work with lambdas because

var result = list.Where(x => x % 2 == 0);

is much more concise then

var result = list.Where(IsEven);

public bool IsEven(int i)
{
    return i%2 == 0;
}

An other usage are expression trees.

In your case a normal method is the better choice regarding readability/maintainability.

share|improve this answer
1  
Edit your answer please. You missed a bit :) –  leppie Jul 20 '11 at 9:52
    
ops, sorry :) damn copy&paste errors –  Zebi Jul 20 '11 at 9:54

You can access (capture) the local variables of the calling function LoginAuthenticate from within getDestinationPage and invalidLogin. invalidLogin captures e. If you make this a method, you have to pass e as an additional parameter.

If you don't need this, it is indeed cleaner to just declare a private method.

share|improve this answer

Methods are basically "what the object can do". Delegates are the mechanism to decide "what to do" and "do it" in different places. It's like making premature decisions when comparing to if(a) then b(); else c();

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.