Property Blocks in C#

Property Blocks:
Functionality related to selector and modifier functions is provided in the form of property blocks. Property blocks are defined for some member data attributes of a class.
Read-Only Property   {get block}
Write-Only Property {set block}
By using property blocks we access value of private data members of a class into another class.
Example:
Let’s see an example here are two classes one is the form class in which we will use the property blocks defined in another class. A sample calculator is designed and one class is added to the solution named as arithmetic. In this class property blocks are defined and methods are defined for sum, multiplication, division and subtraction. In the form class property blocks are used to assign and return the value of data members of arithmetic class. First data members of arithmetic class are assigned values using property blocks then methods are called on click of button and then result is assigned to a result label using property block for result data member.
·         Start a new WindowsFormApplication.
·         Add five buttons, two textboxes and one label in which result will be displayed on the form which will appear.
·         Go to Project tab.
·         Select Add Class. Name it with .cs extension.
·         Now in this class define data members, property blocks for data members and methods which will use these data members.
·         Now define functionality for the buttons.  
Here is code for arithmetic class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SampleCalculator
{
    class arithmetic
    {
        private int num1, num2, result;
        public arithmetic()
        {
            num1 = 0;
            num2 = 0;
            result = 0;
        }
        public int FirstNum
        {
            get
            {
                return num1;
            }
            set
            {
                num1 = value;
            }
        }
        public int SecNum
        {
            get
            {
                return num2;
            }
            set
            {
                num2 = value;
            }
        }
        public int res
        {
            set { res = value; }
            get { return result; }
        }
       
      public void add()
        {
            result = num1 + num2;
        }
       public void sub()
        {
            result = num1 - num2;
        }
       public void mul()
        {
            result = num1 * num2;
        }
      public  void divide()
        {
            result = num1 / num2;
        }
        public arithmetic(int x, int y)
        {
            num1 = x;
            num2 =y;
          

           
        }
       


    }

}
            
Now here is code for the form class.
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 SampleCalculator
{
    public partial class Form1 : Form
    {
        arithmetic ar1 = new arithmetic();
        public Form1()
        {
            InitializeComponent();
        }

        private void addbtn_Click(object sender, EventArgs e)
        {
           
            int n1, n2;
            n1 = int.Parse(num1txtbx.Text);
            n2 = int.Parse(num2txtbx.Text);
          //Property Blocks used to assign value of num1&num2 declared in arithmetic class
ar1.FirstNum = n1;  
            ar1.SecNum = n2;

            ar1.add();

            rsltlbl.Text = ar1.res.ToString();//Value returned from property block
        }
        private void Mulbtn_Click(object sender, EventArgs e)
        {
           
            int n1, n2;
            n1 = int.Parse(num1txtbx.Text);
            n2 = int.Parse(num2txtbx.Text);

            ar1.FirstNum = n1;
            ar1.SecNum = n2;

            ar1.mul();

            rsltlbl.Text = ar1.res.ToString();
        }
private void divbtn_Click(object sender, EventArgs e)
        {

            int n1, n2;
            n1 = int.Parse(num1txtbx.Text);
            n2 = int.Parse(num2txtbx.Text);

            ar1.FirstNum = n1;
            ar1.SecNum = n2;

            ar1.divide();

            rsltlbl.Text = ar1.res.ToString();


        }
private void subbtn_Click(object sender, EventArgs e)
        {
                  
            int n1, n2;
            n1 = int.Parse(num1txtbx.Text);
            n2 = int.Parse(num2txtbx.Text);

            ar1.FirstNum = n1;
            ar1.SecNum = n2;

            ar1.sub();

            rsltlbl.Text = ar1.res.ToString();

        }

      }
}

This type of application is called two-tier based application.
Presentation Layer (Form Class)
Business Layer (arithmetic Class)








  


Comments

Popular posts from this blog

Using Progress Bar In C#

Get elements by class name in javascript

Jquery serer side datatables in asp.net