Object A + Object B , What ! Yes it's Possible

Have you tried  before to make arithmetic operations on Objects, if you did sure the compiler Flashed an error  but that does not mean that we can't  add object to another or subtract, that is possible by overloading operators by user-defined class; that 's what I will demonstrate  right now :

Class Money :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Object_A_plus_Object_B

{

    class Money

    {

        private int income = 0; //Field

        private int spent = 0;//Field

        public string getIncome

        {  //proprety returning Income field vale as a string

            get { return this.income.ToString(); }

        }

        public string getSpent //proprety returning Income field vale as a string

        {

            get { return this.spent.ToString(); }

        }

 

        public Money(int Income, int Spent)//Constructor

        {

            this.income = Income;

            this.spent = Spent;

        }

 

        public static Money operator +(Money m1, Money m2)   //overloading + operator and fdefine how the addition has to be done 

        {

            return new Money(m1.income + m2.income, m1.spent + m2.spent);

        }

    }

}

 
Form 1 behind code

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Object_A_plus_Object_B

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Go_Click(object sender, EventArgs e)

        {

            try

            {

                int objectAIncome = Convert.ToInt16(a1.Text);

                int objectASpent = Convert.ToInt16(a2.Text);

                int objectBIncome = Convert.ToInt16(b1.Text);

                int objectBSpent = Convert.ToInt16(b2.Text);

                Money ObjectA = new Money(objectAIncome, objectASpent);

                Money ObjectB = new Money(objectBIncome, objectBSpent);

                Money ObjectC = ObjectA + ObjectB;

                c1.Text = ObjectC.getIncome;

                c2.Text = ObjectC.getSpent;

            }

            catch (Exception ex)

            {

                MessageBox.Show("Error , Please enter int values !");

            }

        }

    }

}

 
UI


Image1.png


Let's  explain the code :

  1. We created a Class called Money
  2. We defined how the addition of two instantiated objects of Money class should be operated : object a + abject b returns another object from the class Money Object C it's income field =object A.income field  + object B.income field and it's spent field =object A.spent field  + object B.spend field

To understand more download the source and analyze it carefully , I tried to make it easy to get , leave a comment and  good luck