Monday, February 28, 2011

Add item in DropDownlist in JavaScript

  function AddItems(ddlName, textField, ValueField) {
            var opt = document.createElement("option");
            opt.text = textField;
            opt.value = ValueField;
            ddlName.options.add(opt);
        }

Method Overriding and Method Hiding in C#

Method Overriding and Method Hiding in C#
One of the most important oops concepts in .NET is Abstraction that literally means 'act of representing only essential features without including background details or explanations.' C# conforms well to this concept and provides various useful ways to implement this concept.
Note:This article may be considered as a foundation for the next article to be published regarding 'Abstract Classes and Interfaces in C# and differences with Overriding and Hiding
Lets first understand the use of 'virtual' keyword:
Method Overriding:
In some situations we want to have a class that has types(specially methods) that can possibly be modified in the derived class. In such situation we declare the method in the base class as virtual and precede the same method in the derived class with 'override' keyword
Consider the following code in which situation is that, a base class method Area() returns square of a parameter and suppose we want to extend this method in the derived class sothat this square may just be multiplied by a constant pi(3.142) so that it can return area of a circle:

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
      public class VirtualDemo
      {
            public virtual double Area(double r)
            {
               return r * r;
            }
      }
      public class A : VirtualDemo
      {
            public override double Area(double r)
            {
               double p = 3.142;
               return base.Area(r) * p;
            }
      }
      public class Test
      {
            public static void Main(string[] args)
            {
               A obj1 = new A();
               Console.WriteLine(obj1.Area(3));
               Console.ReadLine();
            }
      }
}
Output:
28.278
KeyPoints:

~virtual method must have its implementation in the base class and may optionally be overrided in the derived class if some additional functionality is required
~signature of the method in base as well as derived class should be the same
Now lets understand Method hiding by slightly modifying the same code:
Method Hiding:
In some situation we may want to have a method in the base class which can be implemented in the derived class independent of the method in the base class i.e we may want to have its new version altogether
In the following code i add a method Hello() in the base class and in the derived class give a completely new definition to the same method by preceding it with 'new' keyword

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
           public class VirtualDemo
            {
                  public virtual double Area(double r)
                  {
                        return r * r;
                  }
                  public void Hello()
                  {
                        Console.WriteLine("Hello in Base Class");
                  }
            }
         public class A : VirtualDemo
         {
               public override double Area(double r)
               {
                  double p = 3.142;
                  return base.Area(r) * p;
               }
               public new void Hello()
               {
                  Console.WriteLine("Hello in Derived Class");
               }
         }
         public class Test
          {
            public static void Main(string[] args)
            {
                  A obj1 = new A();
                  Console.WriteLine(obj1.Area(3));
                  obj1.Hello();
                  Console.ReadLine();
            }
         }
}

Output:
28.278
Hello in Derived Class

KeyPoints:
~if any class method doesnot have 'virtual','abstract' or 'override' keyword and we attempt to have its new implementation in any derived class a warning is generated to supply 'new' keyword to the method in the derived class.We can also not override that class method unless that method has 'virtual' keyword
That was in brief about Method Overriding. In case of any query ask me on the same blog. In my next article i will explain Abstract Classes and Interfaces with examples and how these all concepts relate to each other and differ from each other with similar and even better approach.
Ask me if you have any query so far after reading the article.

Thursday, February 24, 2011

Event Bubbling with User Control


//User  Control

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    public event EventHandler BubbleClick;


    override protected void OnInit(EventArgs e)
    {
        InitializeComponent();
        base.OnInit(e);
    }

    private void InitializeComponent()
    {
        this.Button1.Click += new EventHandler(Button1_Click);
        this.Load += new System.EventHandler(this.Page_Load);

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("WebUserControl1 :: Page_Load <BR>");

    }


    public void Button1_Click(object sender, EventArgs e)
    {
        OnBubbleClick(e);
    }
    protected void OnBubbleClick(EventArgs e)
    {
        if (BubbleClick != null)
        {
            BubbleClick(this, e);
        }
    }
    #region Web Form Designer generated code
   
  

  
    #endregion
}


//Page Control
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
          Response.Write("PageControl1 :: Page_Load <BR>");
    }

    override protected void OnInit(EventArgs e)   //page OnInit
    {

        try
        {

            InitializeComponent();
            base.OnInit(e);
        }
        catch (Exception ex)
        {


        }
    }
    private void InitializeComponent()
    {

        try
        {

            // this.Load += new System.EventHandler(this.Page_Load);
            Btn.BubbleClick += new EventHandler(Btn_BubbleClick);

        }
        catch (Exception ex)
        {

        }
    }
    public void Btn_BubbleClick(object sender, EventArgs e)  //Click Event
    {

        Response.Write("PAge Btn_BubbleClick :: Page_Load <BR>");

    }
}