Menus

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, May 8, 2014

How To Implement Back To Top Feature in Html/Asp.Net

In many websites having long contents we have seen a “Back To Top” or “Scroll To Top” button when clicked takes you to the top of the webpage. We see this kind of feature normally on the product listing pages of e-commerce websites. Today in this post we will implement the same feature in one of the easiest possible way. To integrate this feature, we need a little HTML, some CSS  for styling and a couple of lines of jQuery code. Using the below method, you can integrate the “Back To Top” feature on your website irrespective of the programming language of your website, be it ASP.Net or PhP.
To start with, we will create a HTML page with the following content in the <body> section.
 
<body>
<h1>Back To Top Demo by Nitesh Luharuka</h1>
<div style="height:1000px"></div>
<span id="back-to-top"><img src='images/up-arrow.png'/></span>
</body>
 
If you notice the above, we have added a div with some height to test our feature. At the end, we have added a “<span>” tag having an image of upward arrow. This arrow will be shown once the user starts scrolling down. To position the image, we will use a bit of CSS.
Here’s the CSS for the button. You need to add this in the head section of your page.
#back-to-top {
     position: fixed;
     bottom: 30px; top: 350px;
 }
#back-to-top img {
 cursor:pointer;
}
Now, our HTML is ready and for adding the code to the image, we will write the below code in our <head> section of the page. If you’ve already included jQuery, then you do not need to include jQuery file again in your project.
<script type="text/javascript">
$(document).ready(function () {
//Hide Back to top button
       $("#back-to-top").hide();
       $(window).scroll(function () {
                            if ($(window).scrollTop() > 200) {
                           $('#back-to-top').fadeIn();
                          }
                         else {
                         $('#back-to-top').fadeOut();
                            } });

 $('#back-to-top img').click(function () {
 $('body').animate({
            scrollTop: 0 }, 1000);
            });
        });
</script>
In the code above, we have done few things. Let me explain them one by one
  • 1st of all, we hide the image when the document loads.
  • In the scroll() event handler, we have ensured that as the user starts scrolling and has scrolled 200px (you can change this), the “Back To Top” image becomes visible to the user.
  • In the click() event handler of the button, we send the user back to the top of the page using a nice animation effect of jQuery.
 

How To Convert Date Time to “X minutes ago” in jQuery

Today I found a nice plugin with the help of which you can convert any date time on your HTML page to something similar to Gmail/Facebook updates – “5 minutes ago” or “a day ago”. The best part of this plugin is it auto updates the minutes as you’re on the webpage. So, if you have opened the page now and the date shows “1 minute ago”, after 5 minutes the same date will auto update to “6 minutes ago”. In this post, we will see how this plugin works and write a simple HTML using the plugin.

To start with, you can download the timeago plugin from here.This plugin depends on jquery. so the very 1st thing is to include jQuery in our code and then the plugin Javascript.
 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js">
</script>
<script src="jquery.timeago.js"></script>

Once the plugin files are loaded, you can use the plugin’s timeago() function in any of the following ways -
var date = $.timeago(new Date()); //Displays 'less than a minute ago'
var date = $.timeago('2014-05-04'); //Displays 'a day ago'

You can also call the timeago() function on a specific class on your page as below -
//This will modify the date time on all elements having class as 'time'
$(document).ready(function(){
   $(".time").timeago();
});

How To Convert a Date Time to “X minutes ago” in C#

In this post we will see how we can achieve the same functionality in C#. In this post we will write a C# function that will take a DateTime as a parameter and return us back the appropriate string.
The function to convert DateTime to a “Time Ago” string is as below -
        public static string TimeAgo(DateTime dt)
        {
            TimeSpan span = DateTime.Now - dt;
            if (span.Days > 365)
            {
                int years = (span.Days / 365);
                if (span.Days % 365 != 0)
                    years += 1;
                return String.Format("about {0} {1} ago", years, years == 1 ? "year" : "years");
            }
            if (span.Days > 30)
            {
                int months = (span.Days / 30);
                if (span.Days % 31 != 0)
                    months += 1;
                return String.Format("about {0} {1} ago", months, months == 1 ? "month" : "months");
            }
            if (span.Days > 0)
                return String.Format("about {0} {1} ago", span.Days, span.Days == 1 ? "day" : "days");
            if (span.Hours > 0)
                return String.Format("about {0} {1} ago", span.Hours, span.Hours == 1 ? "hour" : "hours");
            if (span.Minutes > 0)
                return String.Format("about {0} {1} ago", span.Minutes, span.Minutes == 1 ? "minute" : "minutes");
            if (span.Seconds > 5)
                return String.Format("about {0} seconds ago", span.Seconds);
            if (span.Seconds == 0)
                return "just now";
            return string.Empty;
        }
You can call the function something like below-
            Console.WriteLine(TimeAgo(DateTime.Now));
            Console.WriteLine(TimeAgo(DateTime.Now.AddMinutes(-5)));
            Console.WriteLine(TimeAgo(DateTime.Now.AddMinutes(-59)));
            Console.WriteLine(TimeAgo(DateTime.Now.AddHours(-2)));
            Console.WriteLine(TimeAgo(DateTime.Now.AddDays(-5)));
            Console.WriteLine(TimeAgo(DateTime.Now.AddMonths(-3)));
The output looks something like below -
datetime-time-ago-C#

Tuesday, April 30, 2013

enum in c# .NET


What is enum?

An enum is a value type with a set of related named constants often referred to as an enumerator list. The enum keyword is used to declare an enumeration. It is a primitive data type, which is user defined.

Enums type can be integer (float, int, byte, double etc.). But if you used beside int it has to becast.

enum is used to create numeric constants in .NET framework. All member of enum are of enum type. There must be a numeric value for each enum type.

The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

enum Dow {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

Program to demonstrate how to create and Use an Enum:

using System;

namespace example_enum
{
    class Program
    {
        public enum DayofWeek
        {
            Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Sunday,DayofWeek.Sunday);
            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Monday,DayofWeek.Monday);
            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Tuesday,DayofWeek.Tuesday);
            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Wednesday,DayofWeek.Wednesday);
            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Thursday,DayofWeek.Thursday);
            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Friday,DayofWeek.Friday);
            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.Saturday,DayofWeek.Saturday);
            Console.ReadLine();
        }
    }
}

Some points about enum: 
  • enums are enumerated data type in c#.
  • enums are not for end-user, they are meant for developers.
  • enums are strongly typed constant. They are strongly typed, i.e. an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same.
  • Enumerations (enums) make your code much more readable and understandable.
  • enum values are fixed. enum can be displayed as a string and processed as an integer.
  • The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.
  • Every enum type automatically derives from System.Enum and thus we can use System.Enum methods on enums.
  • Enums are value types and are created on the stack and not on the heap.
You give two same values in enum type?

Yes we can have same value in enum type. Example when we want to set priority options like

Normal                       0
Excellent                    1
Default                      0
Good                         3

Program showing enum type having same values


using System;

namespace enum_example4
{
    class Program
    {
        public enum DayofWeek
        {
            Sunday = 1, Monday, Tuesday = 1, Wednesday, Thursday = 2, Friday, Saturday
        }


        static void Main(string[] args)
        {
            string[] values = Enum.GetNames(typeof(DayofWeek));
            foreach (string s in values)
            {
                Console.WriteLine(s);
            }

            Console.WriteLine();

            int[] n = (int[])Enum.GetValues(typeof(DayofWeek));
            foreach (int x in n)
            {
                Console.WriteLine(x);
            }
            Console.ReadLine();
        }
    }
}

Program to find out number of values in enum


using System;

namespace enum_exampl3
{
    class Program
    {
        public enum DayofWeek
        {
            Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
        }

        static void Main(string[] args)
        {
            string[] values = Enum.GetNames(typeof(DayofWeek));
            int total = 0;
            foreach (string s in values)
            {
                Console.WriteLine(s);
                total++;
            }
            Console.WriteLine("Total values in enum type is : {0}", total);
            Console.WriteLine();

            int[] n = (int[])Enum.GetValues(typeof(DayofWeek));
            foreach (int x in n)
            {
                Console.WriteLine(x);
            }
            Console.ReadLine();
        }
    }
}

Above code review:


In the above program you have string and numeric representation of enum values. Enum class contains many useful methods for working with enumerations. The beauty of enum is that you can process it as integer value and display as string.

Generic Types in C#


Generic Types

Generics are the most powerful feature of C# 2.0. It allows defining type-safe data structures, without committing to actual data types. In C# 1.0 we can either declare reference type or value type. But in most of the application we come across situation where we need type that can hold both reference & value type. In such situation we use generic types.

Why Generics?
  1. Generic type doesn't care what the type is. We can specify the type at runtime.
  2. It avoids boxing difficulties. In C# 1.0, if we want to put any object into a List, Stack, or Queue objects, we have to take type as System.Object.
  3. It boosts the performance of the application because we can reuse data processing algorithm without duplicating type-specific code.
How Generic implemented:

(1)    Generic type is instantiated at run-time not compiled time
(2)    Generic type are checked at time of declaration not at instantiation
(3)    It works for both reference type & value type.

Let's create simple class "GenericList" using C# 1.0 & 2.0 respectively & compare them.


Code GenericList Class (C# 1.0)

using System;
using System.Collections.Generic;
using System.Text; 
public class GenericList
    {
        private  object[] elements;
        private int count; 
        public GenericList()
        {
            elements = new object[10];
        }
        public object this[int index]
        {
            get { return elements[index]; }
            set { elements[index] = value; }
        } 
        public void Add (object parm)
        {
            if (count == elements.Length)
            {
                  // Increase the
                  object[] tmpArray = null ;
                  elements.CopyTo(tmpArray,0);
                  elements = new object[count * 2];
                  elements = tmpArray;                             
            }
            elements[count] = parm;
            count = count + 1;
        }
   }    

Main Method:

static void Main(string[] args)
        {
            Console.WriteLine("using C# 1.0"); 
            GenericList list = new GenericList();
            list.Add(20);        //Argument is boxed
            list.Add(40);        //Argument is boxed
            list.Add("Sixty");   //Error in retrieving
            Console.WriteLine("Item Added"); 
            int val = (int)list[0]; //Casting required
            Console.WriteLine("Value retrived : " + val); 
        }

Memory Consumption

In C# 1.0 boxing is necessary evil to make type system work. While working with structures of System.Collection namespace (Stacks,List,Hashtable etc) we face the problem in insertion & retrieval of values. We need to take System.object as type & System.object is reference type, so whenever we access the hashtable, the runtime has to box the values to put into the collection & need to unbox to take it out.

list.Add(20);        //Argument is boxed

In C# int takes 4 byte but when it boxed it take (4+8) 12 bytes, which is 3 times to normal size.
In C# 2.0 the type is decided at runtime so boxing does not take place.

Type Safe

When we use the statement

list.Add ("Sixty"); or List [3] = "sixty";

It compiles successfully but later on if some one pulls value and cast it into integer it fails. The problem is fixed in C# 2.0; we will get compilation error there.


Code GenericList Class (C# 2.0)

public class GenericList<T>
    {
        public GenericList()
        {
            elements = new T[10]; 
        } 
        private T[] elements;
        private int count;        
        public T this[int index]
        {
            get {return elements [index];}
            set {elements [index] = value;}
        }    
        public void Add (T parm)
        {
            if (count == elements.Length)
            {
                T[] tmpArray = null;
                elements.CopyTo(tmpArray, 0);
                elements = new T [count * 2];
                elements = tmpArray; 
            } 
            elements [count] = parm;
            count = count + 1;  
        } 
    }

Main Method:

static void Main(string[] args)
{Console.WriteLine("using C# 1.0"); GenericList<int> genericList = new GenericList<int>();genericList.Add (10);          //No boxinggenericList.Add (20);          //No boxing
// genericList.Add("Fifty");   //Compile Time ErrorConsole.WriteLine("Item Added"); int valGeneric = (int)genericList[0]; //No Casting RequiredConsole.WriteLine("Value retrived : " + valGeneric); 
}

Some other Points:

(1) Type parameter can be applied to Class, struct, interface & delegates.
 struct Buket<K, V>; interface ICompare<T>
(2)    Type parameter can have constraints.

Constraint
Description
Public class Books where T: struct
The type argument for class Books must be a value type
Public class Books where T: class
The type argument for class Books must be a reference type
Public class Books where T: new( )
The type argument for class Books must have a public default constructor
Public class Programmer where T: <Employee>
The type argument for class Programmer must be of Employee type.