Search code examples
c#settergetteraccessormutators

Using setters and getters for math equations and printlines


So I'm having trouble using setter and getters I suppose as I'm not able to get the value of addNumbers right now I have the others commented out so I'm not worried about them at the moment. I'm mostly concerned about the syntax needed to writeline the value for addnumbers once I pass it into the setter, retrieve if from the getter and then use it in addNumbers. If someone could enlighten me or give me a decent example it would be better than were I am currently. Thanks much for your help in advance!

class MainModule
    {
        static void Main(string[] args)
        {
            Info myInfo = new Info();
            myInfo.DisplayInfo("Assignment 3A - Math Ops w/ Properties");

            MathOperations ops = new MathOperations();

            int input1, input2;
            Console.WriteLine("Enter first number to calculate");
            ops.operand1 = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter second number to calculate");
            ops.operand2 = int.Parse(Console.ReadLine());

            Console.WriteLine("Added: {0}", ops.addNumbers);
            //Console.WriteLine("Subtracted: " + ops.subtractNumbers(value1, value2));
            //Console.WriteLine("Divided: " + ops.divideNumbers(value1, value2));
            //Console.WriteLine("Multiplied: " + ops.multiplyNumbers(value1, value2));

            Console.ReadLine();
        }


class MathOperations
    {
        private int operand1;
        private int operand2;

        public int Operand1
        {
            get
            {
                return operand1;
            }
            set
            {
                operand1 = value;
            }
        }
        public int Operand2
        {
            get
            {
                return operand2;
            }
            set
            {
                operand2 = value;
            }
        }
        public MathOperations()
        {
            //default constructor
        }
        public MathOperations(int op1, int op2)
        {
            operand1 = op1;
            operand2 = op2; 
        }
        public int addNumbers()
        {
            return operand1 + operand2;
        }

Solution

  • addNumbers is not a property as defined above. It is a method and you need to add the parenthesys

     Console.WriteLine("Added: {0}", ops.addNumbers());
     .....
     public int addNumbers()
     {
         return operand1 + operand2;
     }
    

    otherwise, but I don't recommend it because you are executing an operation not reading an attribute of your class, change your code to

       Console.WriteLine("Added: {0}", ops.addNumbers);
       .....
       public int addNumbers
       {
            get{ return operand1 + operand2; }
       }
    

    in this way you have a readonly property for your getter. However I still think that a property should express an attribute of your class not an operation, so the implementation as method seems to be more correct in this case.