Search This Blog

Monday, 18 January 2016

Method overriding Vs Method Hiding in c sharp

Method overriding – Using override keyword in child class, Re-implementing the logic of base class method and hiding the base method.

Method Hiding – Using new keyword in child class hiding the own method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodOverridingVsHiding
{
    class BaseClass
    {
        public BaseClass()
        {
            Console.WriteLine("Base Class Constructor.");
        }

        public void Print()
        {
            Console.WriteLine("Base Class Print Method.");
        }

        public void BaseDisplay()
        {
            Console.WriteLine("Base Class Display Method.");
        }
    }

    class ChildClass : BaseClass
    {
        public ChildClass()
        {
            Console.WriteLine("Child Class Constructor.");
        }

        public void Print()
        {
            Console.WriteLine("Child Class Print Method.");
        }

        public void ChildDisplay()
        {
            Console.WriteLine("Child Class Display Method.");
        }

        static void Main(string[] args)
        {
            // create object here

            Console.ReadLine();
        }
    }
}

Step 1:- Create object of Base Class

BaseClass objBaseClass = new BaseClass();
objBaseClass.Print();
objBaseClass.BaseDisplay();

Output

Base Class Constructor.
Base Class Print Method.
Base Class Display Method.

Note:-

1.  If you create the object of the base class, it will access all method in the base class.
2.  If the class using constructor, it will call first and then the method calling sequence.

Step 2:- Create object of Child Class

ChildClass objChildClass = new ChildClass();
objChildClass.Print();
objChildClass.BaseDisplay();
objChildClass.ChildDisplay();

Output

Base Class Constructor.
Child Class Constructor.
Child Class Print Method.
Base Class Display Method.
Child Class Display Method.

Note:-

1.  If a child class inherits the base class and you are creating the object of the child class, its access all method of the base class as well as its own method.
2.  If base class any method with same name exists in the child class, it’s called the own method.
3.  If you want to still call the base method use the new keyword, new keyword tells the compiler to hide the child method and call the base method.
4.  If the classes using constructor, it will call first base class constructor and then the child constructor.
             
Note (For Point 3):

1.  If you are using the new keyword in child class for hiding method intentional, check base class method marked virtual or not.
2.  If base class method not marked virtual, still it's called child class method.

Do following Changes:

In Base class mark Print method as virtual
        public virtual void Print()
        {
            Console.WriteLine("Base Class Print Method.");
        }
In Child class mark Print method as new
        public new void Print()
        {
            Console.WriteLine("Base Class Print Method.");
        }


Step 3:- Create object of Base Class that reference to object of Child class

BaseClass objBaseChild = new ChildClass();
objBaseChild.Print();
objBaseChild.BaseDisplay();

Output

Base Class Constructor.
Child Class Constructor.
Base Class Print Method.
Base Class Display Method.

Note:-

1.  If you create the object of the base class that reference to object of a child class, its access the all method of the base class as well as overriding methods of the child.
2.  If base class any method with same name exists in the child class, it’s called the base class method.
3.  If you want to still call the child method use the override keyword, override keyword tells the compiler to hide the base method and call the child method.
4.  If the classes using constructor, it will call first base class constructor and then the child constructor.

Note (For Point 3):

1.  If you are using the override keyword in child class, check base class method marked virtual or not.
2.       If base class method not marked virtual, you will get the runtime error.

Do following Changes:

In Base class mark Print method as virtual
        public virtual void Print()
        {
            Console.WriteLine("Base Class Print Method.");
        }
In Child class mark Print method as new
        public override void Print()
        {
            Console.WriteLine("Base Class Print Method.");

        }

Saturday, 16 January 2016

Response.Redirect Vs Server.Transfer Vs Server.Execute in Asp.Net

 Response.Redirect
  1. Page URL change in the browser
  2. Redirect a request to new URL and Specifies the new URL
  3. It's used to navigate to sites/pages on the same web server or outside of the web server.
  4. Response.Redirect has overloaded parameter i.e. Boolean by default it's true.
  5. If you don't specify the overloaded parameter during redirection, it's terminate the execution of the current page and Redirect a request to the new URL.
  6. If you need to execution of current page operation during redirection, set overloaded parameter false, your code look like-
  7. Response.Redirect("~/WebForm2.aspx"false);
To Test Scenario, use Visual Studio debugger-

Step 1:- WebForm1.aspx

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Redirect("~/WebForm2.aspx");
    Response.Write("Welcome Back");           
}

Task 1:- Click button

You will redirected to WebForm2.aspx, you will see current page Response.Write line not executed means current page execution terminated during redirection.

Task 2:- Change Response.Redirect("~/WebForm2.aspx") to Response.Redirect("http://www.google.com/")  and Click button

You will redirected to google.com

Task 3:- Change Response.Redirect("~/WebForm2.aspx") to Response.Redirect("~/WebForm2.aspx", false) and Click button

You will see, you are redirected to WebForm2.aspx as well as current page Response.Write line executed.

Server.Transfer
  1. First page URL not change in the browser
  2. Redirect a request to new URL by using the specified URL path of the page.
  3. It's used to navigate to sites/pages on the same web server.
  4. Server.Transfer has overloaded parameter i.e. Boolean by default it's true.
  5. Overloaded parameter used to preserve form data during redirection.
  6. Server.Transfer terminates the execution of the current page and start the execution of the new page
  7. If you don't want to preserve form data during redirection, set overloaded parameter false, your code look like-
  8. Server.Transfer("~/WebForm2.aspx"false);
To Test Scenario, use Visual Studio debugger-

Step 1:- WebForm1.aspx

protected void Button1_Click(object sender, EventArgs e)
{
    Server.Transfer("~/WebForm2.aspx");
    Response.Write("Welcome Back");           
}

Task 1:- Click button

You will redirected to WebForm2.aspx, you will see current page Response.Write line not executed means current page execution terminated during redirection.

Task 2:- Change Server.Transfer("~/WebForm2.aspx") to Server.Transfer("http://www.google.com/")  and Click button

You will get runtime exception, because www.google.com not hosted on the same web server.

Task 3:- Change Server.Transfer("~/WebForm2.aspx") to
Server.Transfer("~/WebForm2.aspx", false) and Click button

You will see, you are redirected to WebForm2.aspx and you will receive a runtime error because you set the preserve form data overloaded parameter false.

To test this scenario, do following modification

In WebForm1.aspx (Add TextBox control in design and set TextBox Text on Button Click)

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = "WebForm1 TextBox";
    Server.Transfer("~/WebForm2.aspx");
    Response.Write("Welcome Back");           
}

In WebForm2.aspx (on Page load try to retrieve WebForm1 TextBox control value)

System.Collections.Specialized.NameValueCollection obj = Request.Form;
Response.Write(obj["TextBox1"].ToString());


Server.Execute

  1. First page URL not change in the browser
  2. Redirect a request to new URL by using the specified URL path of the page.
  3. It's used to navigate to sites/pages on the same web server.
  4. Server.Execute has few overloaded parameter.
  5. One of overloaded parameter used to preserve form data during redirection.
  6. Sertver.Execute process the second Web form without leaving the first Web Form. After completing the execution of the second web form, the control returns to the first web form.
  7. If you don't want to preserve form data during redirection, set overloaded parameter false, your code look like-
  8. Server.Execute("~/WebForm2.aspx"false);


To Test Scenario, use Visual Studio debugger-

Step 1:- WebForm1.aspx

protected void Button1_Click(object sender, EventArgs e)
{
    Server.Execute("~/WebForm2.aspx");
    Response.Write("Welcome Back");           
}

Task 1:- Click button

You will see, Sertver.Execute process the second Web form without leaving the first Web Form. After completing the execution of the second web form, the control returns to the first web form and executed the next line of code.

Task 2:- Change Server.Execute("~/WebForm2.aspx") to Server.Transfer("http://www.google.com/")  and Click button

You will get runtime exception, because www.google.com not hosted on the same web server.

Task 3:- Change Server.Execute("~/WebForm2.aspx") to
Server.Execute("~/WebForm2.aspx", false) and Click button

You will see, you are redirected to WebForm2.aspx and you will receive a runtime error because you set the preserve form data overloaded parameter false.

To test this scenario, do following modification

In WebForm1.aspx (Add TextBox control in design and set TextBox Text on Button Click)

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = "WebForm1 TextBox";
    Server.Execute("~/WebForm2.aspx");
    Response.Write("Welcome Back");           
}

In WebForm2.aspx (on Page load try to retrieve WebForm1 TextBox control value)

System.Collections.Specialized.NameValueCollection obj = Request.Form;
Response.Write(obj["TextBox1"].ToString());