Wednesday, January 14, 2015

Delegate As Parameter

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DelegatesAsParameter
{


    public partial class Form1 : Form
    {
        //Delegates as parameter provides a way to pass a method to a method, that is 
        //to have a method as a parameter of another method.
        //

        public delegate bool MyPredicate(int n);

        List<int> intList = new List<int>();
        Random rand = new Random();

        //method to check if n is odd 
        private bool IsOdd(int n)
        {
            return n % 2 != 0;
        }

        private bool IsEvenPositive(int n)
        {
            return n > 0 && n % 2 == 0;
        }

        private bool IsOddNegative(int n)
        {
            return n % 2 != 0 && n < 0;
        }

        //Method to return a list of values (from a collection) that satisfy a given condition

        private List<int> FindAll(MyPredicate predicate)
        {
            List<int> list = new List<int>();
            //write code to return all the odd values of the intList
            foreach (int n in intList)
            {
                //if (IsOdd(n))
                //{
                //    oddList.Add(n);
                //}
                if (predicate(n))
                {
                    list.Add(n);
                }
            }
            return list;

        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //initialize intList with random values
            for (int i = 1; i <= 50; i++)
            {
                intList.Add(rand.Next(-99, 100));
            }
            Display(intList);
        }

        //display method to display any list
        private void Display(List<int> list)
        {
            listBox1.Items.Clear();
            foreach (int n in list)
                listBox1.Items.Add(n);
             
        }

        private void btnGetOddValues_Click(object sender, EventArgs e)
        {
            MyPredicate predicate = IsOdd;
            List<int> oddList = FindAll(predicate);

            Display(oddList);
        }

        private void btnEvenPositive_Click(object sender, EventArgs e)
        {
            MyPredicate predicate = IsEvenPositive;
            List<int> oddList = FindAll(predicate);

            Display(oddList);
        }

        private void btnOddNegatives_Click(object sender, EventArgs e)
        {
            MyPredicate predicate = IsOddNegative;
            List<int> oddList = FindAll(predicate);

            Display(oddList);
        }
    }
}
//Lab_
//Read about anonymous methods
//replace the first statement of each button click by associating the 
//delegate object predicate to an anonymous method.

No comments:

Post a Comment