Why no variable to store method return value (Delegates and Methods)

Answered Why no variable to store method return value (Delegates and Methods)

  • Donnerstag, 16. August 2012 16:39
     
      Enthält Code

    public int CalculateProduct(int num1, int num2) Requires a return type of int. However, in main, when CalculateProduct is called, no variable is assigned to accept the int being returned. I don't understand why. Thanks!

    using System;
    delegate void NotifyCalculation(int x, int y, int result);
    class Calculator {
        NotifyCalculation calcListener;
        public Calculator(NotifyCalculation listener) {
            calcListener = listener;
        }
        // THIS IS NOT A VOID. IT HAS A RETURN TYPE OF INT
        public int CalculateProduct(int num1, int num2) {
            // perform the calculation
            int result = num1 * num2;
            // notify the delegate that we have performed a calc
            calcListener(num1, num2, result);
            // return the result
            return result;
        }
    }
    class CalculationListener {
        public static void CalculationPrinter(int x, int y, int result) {
            Console.WriteLine("Calculation Notification: {0} x {1} = {2}", 
                x, y, result);
        }
    }
    class Listing_04 {
        static void Main(string[] args) {
            // create a new Calculator, passing in the printer method
            Calculator calc = new Calculator(CalculationListener.CalculationPrinter);
            // THE RETURN TYPE IS NOT BEING CAPTUREED HERE WHY?
            calc.CalculateProduct(10, 20);
            calc.CalculateProduct(2, 3);
            calc.CalculateProduct(20, 1);
            // wait for input before exiting
            Console.WriteLine("Press enter to finish");
            Console.ReadLine();
        }
    }

Alle Antworten

  • Donnerstag, 16. August 2012 16:59
    Moderator
     
     Vorgeschlagene Antwort Enthält Code

    Your code works fine - if you're asking why CalculateProduct must be declared as:

    public int CalculateProduct(int num1, int num2) 

    This is because you're returning the result:  "return result;"

    If you want to declare it as void, you can - just don't return anything:

       public void CalculateProduct(int num1, int num2)
        {
            // perform the calculation
            int result = num1 * num2;
            // notify the delegate that we have performed a calc
            calcListener(num1, num2, result);
            // Don't return the result
            // return result;
        }

    This will give the same results as your original code (since you never used the return value).


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Donnerstag, 16. August 2012 17:02
     
     Beantwortet

    Hi Jacob;

    Just because a function returns a value does not require you to capture it in a variable, by not capturing the value it is thrown away.

    You state, "public int CalculateProduct(int num1, int num2) Requires a return type of int", only because the function signature defines one. In this case you can remove the int return type in the signature and comment out the return statement and all will still work.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Dienstag, 21. August 2012 15:31
     
     

    thanks guys! Very much appreciated. I'm from a VB6 background and from what i can remember, functions in VB6 required that the return value be captured.

    Thanks for answering my question.

  • Dienstag, 21. August 2012 16:18
     
     

    thanks guys! Very much appreciated. I'm from a VB6 background and from what i can remember, functions in VB6 required that the return value be captured.

    You need to capture the result if you use the function call syntax.

    Function F(i As Integer)

    can be called

    x = F(42) 'capturing the result

    or

    F 42 'not capturing the result

    A function without parameters can be called

    x = F 'capturing the result

    or

    F 'not capturing the result

  • Dienstag, 21. August 2012 16:42
     
     
     

    Not a problem, Glad I was able to help.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".