Sunday, February 18, 2018

Class Inheritance

Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. Through effective employment of reuse, you can save time in your programming.


Create BaseClass.cs


using System;

public class ParentClass
{
    public ParentClass()
    {
        Console.WriteLine("Parent Constructor.");
    }

    public void print()
    {
        Console.WriteLine("I'm a Parent Class.");
    }
}

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

    public static void Main()
    {
        ChildClass child = new ChildClass();

        child.print();
    }
}
Output:
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Shows two classes. The top class is named ParentClass and the main class is called ChildClass. What we want to do is create a child class, using existing code from ParentClass.
First we must declare our intention to use ParentClass as the base class of ChildClass. This is accomplished through the ChildClassdeclaration public class ChildClass : ParentClass. The base class is specified by adding a colon, “:”, after the derived class identifier and then specifying the base class name.
Note: C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from. However, it does allow multiple interface inheritance, a subject covered in a later lesson.
ChildClass has exactly the same capabilities as ParentClass. Because of this, you can also say ChildClass “is” a ParentClass. This is shown in the Main() method of ChildClass when the print() method is called. ChildClass does not have its own print() method, so it uses theParentClass print() method. You can see the results in the 3rd line of output.
Base classes are automatically instantiated before derived classes. Notice the output from Listing . The ParentClass constructor executed before the ChildClass constructor.
Thank you

Saturday, February 17, 2018

State Management in ASP.Net

In this article I tried to briefly summarize the concept of State Management 

State Management | Types
In ASP.NET there are the following 2 State Management methodologies:
  • Client-side Management:-Whenever we use Client-Side State Management, the state related information will directly get stored on the client-side. That specific information will travel back and communicate with every request generated by the user then afterwards provides responses after server-side communication.
Client-side Techniques
  • View
  • Hidden
  • Cookies
  • Control State
  • Query Strings
  • Server-side Management :-Server-Side State Management is different from Client-Side State Management but the operations and working is somewhat the same in functionality. In Server-Side State Management all the information is stored in the user memory. Due to this functionality there is more secure domains at the server side in comparison to Client-Side State Management.
  • Server-side Techniques 
    • Session State
    • Application State
State | Overview
As we all know, browsers are generally stateless.

Now question arises here, what does stateless actually mean?

Stateless means, whenever we visit a website, our browser communicates with the respective server depending on our requested functionality or the request. The browser communicates with the respective server using the HTTP or HTTPs protocol.

But after that response, what's next or what will happen when we visit that website again after closing our web browser?

In this case HTTP/HTTPs doesn't remember what website or URL we visited or in other words we can say it doesn't hold the state of a previous website that we visited before closing our browser, that is called stateless.

Now I guess you have at least an idea of what state and stateless actually means.

So our browsers are stateless.


State Management | Scenario

It will be a little difficult to directly evaluate what will be better for our application. We cannot directly say that we will use client-side or server-side architecture of State Management.

State Management | Techniques

State Management techniques are based on client side and server side. Their functionality differs depending on the change in state, so here is the hierarchy:

View State

In general we can say it is used for storing user data in ASP.NET, sometimes in ASP.NET applications the user wants to maintain or store their data temporarily after a post-back.. In this case VIEW STATE is the most used and preferred way of doing that.

This property is enabled by default but we can make changes depending on our functionality, what we need to do is just change the EnableViewState value to either TRUE for enabling it or FALSE for the opposite operation.



Figure: [View State Management]

Snippet
  1. // Page Load Event  
  2. protected void Page_Load(object sender, EventArgs e)  
  3. {  
  4.     if (IsPostBack)  
  5.     {  
  6.         if (ViewState["count"] != null)  
  7.         {  
  8.             int ViewstateVal = Convert.ToInt32(ViewState["count"]) + 1;  
  9.             View.Text = ViewstateVal.ToString();  
  10.             ViewState["count"]=ViewstateVal.ToString();  
  11.         }  
  12.         else  
  13.         {  
  14.             ViewState["count"] = "1";  
  15.         }  
  16.     }  
  17. }  
  18.   
  19. // Click Event  
  20. protected void Submit(object sender, EventArgs e)  
  21. {  
  22.        View.Text=ViewState["count"].ToString();  
  23. }  
Points to Remember
Some of the features of view state are:
  • It is page-level State Management
  • Used for holding data temporarily
  • Can store any type of data
  • Property dependent
Hidden Field

A hidden field is used for storing small amounts of data on the client side. In most simple words it's just a container of some objects but their result is not rendered on our web browser. It is invisible in the browser. 

It stores a value for the single variable and it is the preferable way when a variable's value is changed frequently but we don't need to keep track of that every time in our application or web program.



Snippet
  1. // Hidden Field  
  2.   
  3. int newVal = Convert.ToInt32(HiddenField1.Value) + 1;  
  4. HiddenField1.Value = newVal.ToString();  
  5. Label2.Text = HiddenField1.Value;  
Points to Remember
Some features of hidden fields are:
  • Contains a small amount of memory
  • Direct functionality access
Cookies
A set of Cookies is a small text file that is stored in the user's hard drive using the client's browser. Cookies are just used for the sake of the user's identity matching as it only stores information such as sessions id's, some frequent navigation or post-back request objects.

Whenever we get connected to the internet for accessing a specific service, the cookie file is accessed from our hard drive via our browser for identifying the user. The cookie access depends upon the life cycle or expiration of that specific cookie file.


Snippet
  1. int postbacks = 0;  
  2. if (Request.Cookies["number"] != null)  
  3. {  
  4.     postbacks = Convert.ToInt32(Request.Cookies["number"].Value) + 1;  
  5. }  
  6. // Generating Response  
  7. else   
  8. {  
  9.     postbacks = 1;  
  10. }  
  11. Response.Cookies["number"].Value = postbacks.ToString();  
  12.   
  13. Result.Text = Response.Cookies["number"].Value;  
Cookie | Types

Persistent Cookie

Cookies having an expiration date is called a persistent cookie. This type of cookie reaches their end as their expiration dates comes to an end. In this cookie we set an expiration date.

Snippet
  1. Response.Cookies["UserName"].Value = "Abhishek";  
  2. Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(1);  
  3.   
  4. HttpCookie aCookie = new HttpCookie("Session");  
  5. aCookie.Value = DateTime.Now.ToString();  
  6. aCookie.Expires = DateTime.Now.AddDays(1);  
  7. Response.Cookies.Add(aCookie);  
Non-Persistent Cookie

Non-persistent types of cookies aren't stored in the client's hard drive permanently. It maintains user information as long as the user access or uses the services. Its simply the opposite procedure of a persistent cookie.

Snippet
  1. HttpCookie aCookie = new HttpCookie("Session");  
  2. aCookie.Value = DateTime.Now.ToString();  
  3. aCookie.Expires = DateTime.Now.AddDays(1);  
  4. Response.Cookies.Add(aCookie);  
Points to Remember
Some features of cookies are:
  • Store information temporarily
  • It's just a simple small sized text file
  • Can be changed depending on requirements
  • User Preferred
  • Requires only a few bytes or KBs of space for creating cookies
Control State

Control state is based on the custom control option. For expected results from CONTROL STATE we need to enable the property of view state. As I already described you can manually change those settings.

Points to Remember
Some features of query strings are:
  • Used for enabling the View State Property
  • Defines a custom view
  • View State property declaration
  • Can't be modified
  • Accessed directly or disabled
Query Strings

Query strings are used for some specific purpose. These in a general case are used for holding some value from a different page and move these values to the different page. The information stored in it can be easily navigated to one page to another or to the same page as well.


Snippet
  1. // Getting data  
  2. if (Request.QueryString["number"] != null)   
  3. {  
  4.     View.Text = Request.QueryString["number"];  
  5. }  
  6.   
  7. // Setting query string  
  8. int postbacks = 0;  
  9.   
  10. if (Request.QueryString["number"] != null)   
  11. {  
  12.     postbacks = Convert.ToInt32(Request.QueryString["number"]) + 1;  
  13. }  
  14. else   
  15. {  
  16.     postbacks = 1;  
  17. }  
  18.   
  19. Response.Redirect("default.aspx?number=" + postbacks);  
Points to Remember
Some of the features are:
  • It is generally used for holding values
  • Works temporarily
  • Switches info from one to another page
  • Increase performance
  • Uses real and virtual path values for URL routing
I hope you have enjoyed reading this article. I'll write about Server-Side State Management very soon.