8 Replies - 13790 Views - Last Post: 27 July 2012 - 07:17 AM Rate Topic: -----

#1 maurices5000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 26-July 12

Trying to Understand Delegates

Posted 26 July 2012 - 09:53 AM

I'm studying delegates from a book I bought, but I'm still struggling to understand the author's explanation. I understand that delegates can be a difficult topic so I thought I'd come here to get some help with some syntax that I'm not exactly getting. I would appreciate it if you could tell me what is going on.

This code comes from Introducing Visual C# 2010 chapter 10 just for reference.

public delegate int PerformCalc(int x, int y);

class Calculator {
    PerformCalc perfCalc;

    public Calculator() {
        perfCalc = CalculateProduct;
    }

    public PerformCalc CalcDelegate {
        get { return perfCalc; }
    }

    private int CalculateProduct(int num1, int num2) {
        return num1 * num2;
    }
}


I will explain what i understand below and what I don't understand with comments.


//I understand this to be the definition of the delegate. it accepts two integers and returns an int.
public delegate int PerformCalc(int x, int y);

class Calculator {
    //This code defines a new delegate of PerformCalc type.
    PerformCalc perfCalc;

    public Calculator() {
        // I don't understand what this code is doing in this method. Author seems to give conflicting info on this.
        // Is this code assigning the CalculateProduct method to the perfCalc delegate field? 
        // In one statement the author said it was assigning a value.
        // I'm used to method assignment looking like this: public delegate void ProcessBookDelegate(Book book);

        perfCalc = CalculateProduct;
    }

    //This code creates a property of PerformCalc delegate type and it returns a delegate.
    public PerformCalc CalcDelegate {
        get { return perfCalc; }
    }
    
    //This is the method I'm assuming will be used by one of the delegates (I understand the field, perfCalc, to be a delegate and the property CalcDelegate to be a delegate as well).
    private int CalculateProduct(int num1, int num2) {
        return num1 * num2;
    }
}



Thanks for your help!

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to Understand Delegates

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2086
  • View blog
  • Posts: 3,173
  • Joined: 21-June 11

Re: Trying to Understand Delegates

Posted 26 July 2012 - 10:11 AM

Quote

Is this code assigning the CalculateProduct method to the perfCalc delegate field?


Yes.

Quote

I'm used to method assignment looking like this: public delegate void ProcessBookDelegate(Book book);


That's a declaration of a delegate type. It's not an assignment.

The only way to assign something to a variable is to do variable = whatYouWantToAssignToTheVariable. In this case you want the variable perfCalc to refer to the method CalculateProduct, so this is how you do it.
Was This Post Helpful? 1
  • +
  • -

#3 maurices5000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 26-July 12

Re: Trying to Understand Delegates

Posted 26 July 2012 - 10:37 AM

Thanks for your clear explanation. I have one further question.

Can't you assign a method during the delegate declaration similar to the code below?

public delegate void ProcessBookDelegate(Book book);


Perhaps this isn't the right example, but I thought you could declare a delegate and assign a method to it at the time of declaration.

thanks very much. this helps a lot!
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2086
  • View blog
  • Posts: 3,173
  • Joined: 21-June 11

Re: Trying to Understand Delegates

Posted 26 July 2012 - 10:46 AM

We need to distinguish between the declaration of the delegate type and the declaration of any variables of that type.

The line public delegate void ProcessBookDelegate(Book book); declares the delegate type ProcessBookDelegate. You can't assign a value to that type at the declaration or at any other point in the program because a type is not a variable - you can't assign things to a type. There can be many variables of the same type and all can have different values.

The line PerformCalc perfCalc; declares the variable perfCalc (of the delegate type PerformCalc). You can assign a value when you declare a variable. To do so you'd write PerformCalc perfCalc = CalculateProduct;. You would then no longer need to perform the assignment in the constructor.
Was This Post Helpful? 3
  • +
  • -

#5 maurices5000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 26-July 12

Re: Trying to Understand Delegates

Posted 26 July 2012 - 11:14 AM

Thank you. it has taken a little reading and thinking. So I understood "public delegate void ProcessBookDelegate(Book book); " to be creating a delegate. But really it is creating the type. So "PerformCalc perfCalc = CalculateProduct;" is the actual delegate being created?

I don't know how you got the dotted lines around the code.

Thanks I think this has been helpful!
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2086
  • View blog
  • Posts: 3,173
  • Joined: 21-June 11

Re: Trying to Understand Delegates

Posted 26 July 2012 - 11:20 AM

View Postmaurices5000, on 26 July 2012 - 08:14 PM, said:

Thank you. it has taken a little reading and thinking. So I understood "public delegate void ProcessBookDelegate(Book book); " to be creating a delegate. But really it is creating the type. So "PerformCalc perfCalc = CalculateProduct;" is the actual delegate being created?


Yes.

Quote

I don't know how you got the dotted lines around the code.


By using il-tags (for inline code) around it.
Was This Post Helpful? 1
  • +
  • -

#7 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Trying to Understand Delegates

Posted 26 July 2012 - 01:45 PM

I'm sure you understand this, but it's worth remembering that the delegate can be assigned any method(s) that have the same return type and arguments (or parameters, if you prefer). Also, a delegate can hold reference to multiple methods, so you can call the delegate and it will process all its methods in sequence.
Was This Post Helpful? 2
  • +
  • -

#8 maurices5000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 26-July 12

Re: Trying to Understand Delegates

Posted 26 July 2012 - 07:33 PM

Thanks very much guys!
Was This Post Helpful? 0
  • +
  • -

#9 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Trying to Understand Delegates

Posted 27 July 2012 - 07:17 AM

View PostMrShoes, on 26 July 2012 - 01:45 PM, said:

Also, a delegate can hold reference to multiple methods, so you can call the delegate and it will process all its methods in sequence.
Note: If a multicast delegate has a return type then the value returned is from the last delegate method executed. Since they are executed in order added, different results can occur depending on the order in which they are added.
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1