Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2011
    Posts
    26

    addValues or + Operator

    Is there a difference between using the addValues or + Operator?

    eg:

    Code:
    result = addValues(firstNumber, secondNumber);
    seems to do exactly the same job as

    Code:
    result = firstNumber + secondNumber;

  2. #2
    Join Date
    Mar 2004
    Location
    Anacortes, WA
    Posts
    4,168
    Quote Originally Posted by Murkie View Post
    Is there a difference between using the addValues or + Operator?

    eg:

    Code:
    result = addValues(firstNumber, secondNumber);
    seems to do exactly the same job as

    Code:
    result = firstNumber + secondNumber;
    What context is this in? There is no "addValues" method built into .net.
    Need any help? Feel free to PM me - or send an email directly to nelson@3dbuzz.com!

  3. #3
    Join Date
    Apr 2011
    Posts
    26
    Hey Nelson,

    It's taken from the book Visual C# 2010. It's an early chapter introducting calling methods that uses a WPF application to display different type of math operations. The full code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    
    
    namespace MathsOperators
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
    
        public partial class MainWindow : Window
        {
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void calculateClick(object sender, RoutedEventArgs e)
            {
                int calculatedValue = 0;
     
                try
                {
                    int leftHandSide = System.Int32.Parse(lhsOperand.Text);
                    int rightHandSide = System.Int32.Parse(rhsOperand.Text);
    
                    if (addition.IsChecked.HasValue && addition.IsChecked.Value)
                    {
                        calculatedValue = addValues(leftHandSide, rightHandSide);
                        showResult(calculatedValue);
                    }
                    else if (subtraction.IsChecked.HasValue && subtraction.IsChecked.Value)
                    {
                        calculatedValue = subtractValues(leftHandSide, rightHandSide);
                        showResult(calculatedValue);
                    }
                    else if (multiplication.IsChecked.HasValue && multiplication.IsChecked.Value)
                    {
                        calculatedValue = multiplyValues(leftHandSide, rightHandSide);
                        showResult(calculatedValue);
                    }
                    else if (division.IsChecked.HasValue && division.IsChecked.Value)
                    {
                        calculatedValue = divideValues(leftHandSide, rightHandSide);
                        showResult(calculatedValue);
                    }
                    else if (remainder.IsChecked.HasValue && remainder.IsChecked.Value)
                    {
                        calculatedValue = remainderValues(leftHandSide, rightHandSide);
                        showResult(calculatedValue);
                    }
                }
                catch (Exception caught)
                {
                    expression.Text = "";
                    result.Text = caught.Message;
                }
            }
    
            private int addValues(int leftHandSide, int rightHandSide)
            {
                expression.Text = leftHandSide.ToString() + " + " + rightHandSide.ToString();
                return leftHandSide + rightHandSide;
            }
    
            private int subtractValues(int leftHandSide, int rightHandSide)
            {
                expression.Text = leftHandSide.ToString() + " - " + rightHandSide.ToString();
                return leftHandSide - rightHandSide;
            }
    
            private int multiplyValues(int leftHandSide, int rightHandSide)
            {
                expression.Text = leftHandSide.ToString() + " * " + rightHandSide.ToString();
                return leftHandSide * rightHandSide;
            }
    
            private int divideValues(int leftHandSide, int rightHandSide)
            {
                expression.Text = leftHandSide.ToString() + " / " + rightHandSide.ToString();
                return leftHandSide / rightHandSide;
            }
    
            private int remainderValues(int leftHandSide, int rightHandSide)
            {
                expression.Text = leftHandSide.ToString() + " % " + rightHandSide.ToString();
                return leftHandSide % rightHandSide;
            }
    
            private void showResult(int answer)
            {
                result.Text = answer.ToString();
            }
    
            private void quitClick(object sender, RoutedEventArgs e)
            {
                this.Close();
            }
        }
    }
    I guessed that the example was using addValues to demonstrate calling a method, but as it seemed to do exactly the same as using the + operator I was wondering what other uses it had.

    Cheers!

  4. #4
    Join Date
    Jun 2012
    Location
    probably at a PC
    Posts
    309
    Actually... one of the reasons why a method is appropriate here, is quite simply put because it does more than just add two values together.

    It also uses the two number values and constructs some text from it to be used for something other than doing a calculation. It's probably setting some value for something in the XAML portion, so it's (my guess) that it's a means of updating and empty or existing text-field, while also fulfilling it's purpose by returning an integer-typed result of the calculation. (To birds with but one stone, as you will)

    A regular +operation wouldn't be able to do that without some additional lines of code to follow to do the same thing. Obvious disadvantage being... you may have to write the same few lines of code over and over, while calling the method knows how to do that extra job and still work.
    Last edited by nGAGE; 12-11-2013 at 08:21 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •