Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday, August 24, 2011

Runtime Assign Method using Delegate


Writing Plug-in Methods with Delegates




A delegate variable is assigned a method dynamically. This is useful for writing plug-
in methods. In this example, we have a utility method named  Add,Sub that applies
a NumberOper . The NumberOper method has a delegate
parameter,


   ///

        /// delegate  declaration
        ///

        ///
        ///
        ///
        public delegate int NumberOperation(int fvalue, int svalue);
        ///

        /// Button click event it will call NumberOper method
        ///

        ///
        ///
        private void btnClick_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt16(txtfvalue.Text);
            int b = Convert.ToInt16(txtsvalue.Text);
            txtresult.Text = NumberOper(a, b, Add).ToString();//Dynamic we Assign Method name
        }
        ///

        /// Button click event it will call NumberOper method
        ///

        ///
        ///
        private void btnSub_Click(object sender, EventArgs e)
        {

            int a = Convert.ToInt16(txtfvalue.Text);
            int b = Convert.ToInt16(txtsvalue.Text);
            txtresult.Text = NumberOper(a, b, Sub).ToString(); //Dynamic we Assign Method name

        }

        ///

        /// Main Method with delegate Parameter
        ///

        /// first value
        /// second value
        /// Dynamic Delegate
        /// call method add or sub and return result
        public int NumberOper(int a, int b, NumberOperation AT)
        {

          return   AT(a, b);
        }

        ///

        /// Add two numbers Methos
        ///

        /// first Value
        /// Second value
        /// Result
        public int Add(int a, int b)
        {
            return a + b;
        }
        ///

        /// Sub two number
        ///

        /// first value
        /// Second value
        /// int value
        public int Sub(int a, int b)
        {
            return a - b;
        }



Beginner Delegate program

Add Two Number Delegate Program 

 public delegate int AddTwoNumberDel(int fvalue, int svalue);

        private void btnClick_Click(object sender, EventArgs e)
        {
            AddTwoNumberDel AT = Addnumber;                   // create delegate instance
           //is shorthand for

            //AddTwoNumberDel AT = new AddTwoNumberDel(Addnumber);

           
            int a = Convert.ToInt16(txtfvalue.Text);
            int b = Convert.ToInt16(txtsvalue.Text);
            txtresult.Text = AT(a, b).ToString();          // invoke Method
            //is shorthand for
            //txtresult.Text = AT.Invoke(a, b).ToString();
        }


        public int Addnumber(int fvalue, int svalue)
        {

            return fvalue + svalue;

        }
Note:
Copy and past your VS Editor it will Work... Do you want sample application mail me ...

C# Dotnet Delegate

What is Delegate?

 

·         Its reference to method.

·         once method assign to delegate its work like method

·         delegate method used any other method

 

 Where we will use real time?

 

      If you want to call Function in Runtime .and to get data after you can call your function but method name you don't  know design time  but you know runtime during time  you can use "Delegate"

 

Different type of Delegate:

 

  • Single cast Delegate
  • Multicast Delegate

 

 

Single cast Delegate Syntax:

 

  Below are signature of single cast delegate

 Access-modifier delegate result-type identifier ([parameters]);

Access-modifier è Its Access permission for delegate (like public ,private ..,)

Delegate      è it's a Keyword

Result-type èthis is  return type (like int,string,void)

Identifier è delegate name (user define)

Parameter è run time value passing

 

Ex :

 

Way 1:

 

public delegate void  jsdelegate();

 

above example is no return type ,no parameter

 

Way 2:

 

public delegate string   jsdelegatevalue(int x,int y);

 

above example is string is return type and x,y is parameter

 

above 2 way  are delegate  declaration

 

What is Advantage in VS 2010 Editor?

What is Advantage in VS 2010 Editor?

 

·          There is now support for multiple monitors and the ability to drag windows outside the IDE

·          IntelliSense is now two to five times quicker than previous versions.

  •   Readability of text is improved.
  • Add reference is previously very slow .now is very fast
  • Intelligence now support partial string matching   if you are type SB it will match string StringBuilder also

ExampleJSVS2010.png