Saturday, 8 August 2015

How to count repeated characters in an array

using System;

namespace SequenceCount
{
    class Program
    {
        // to count repeated characters in an array
        private void CountRepeatedCharacters()
        {
            string word = "aaabbcccd";
            char[] characters = word.ToCharArray();
            char startingCharacter = characters[0];
            byte count = 1;
            string output = string.Empty;

            for (int i = 1; i < characters.Length; i++)
            {
                if (characters[i] == startingCharacter)
                {
                    count++;
                }

                else
                {
                    output = output + startingCharacter + count.ToString();
                    // set the current character to startingCharacter and set count to 1
                    startingCharacter = characters[i];
                    count = 1;
                }
            }

            output = output + startingCharacter + count.ToString();
            Console.WriteLine("Output will be : " + output);
            Console.ReadLine();
        }

        static void Main(string[] args)
        {
            Program objProgram = new Program();
            objProgram.CountRepeatedCharacters();
        }
    }
}

Sunday, 26 July 2015

How to know that the Page is Post Back or not without using IsPostBack Property

Current Request Type Property is a way to identify current request is post back request or not.

Current context request object have property named RequestType which can be checked to know that is the page posted back to server or not. For example :-

        if (Request.RequestType == "GET") 
         {
            // Call when page loading first time
            // Equal to if (!IsPostBack) property
         }

        if (Request.RequestType == "POST") 
         {
            // Call when page loading second or higher time
            // Equal to if (IsPostBack) property
         }

This is an alternate way of isPostBack mechanism.


GET :- When user requests to a web page by typing the URL in web browser, open a window through JavaScript (window.open ..) or clicking in hyperlink its called Get request or first time request.


POST :- When user requests to a web page by clicking in button (input type submit) or submit a form through JavaScript to the server (form.submit() function in JavaScript) its called Post request or second / higher request.

That’s it!!…..Happy Programming...

Sunday, 7 June 2015

How to create instance of an interface

In this article, i am going to show what is the way to create an instance of a interface. Actually, there is no direct way to create an instance of a interface. This is because they are incomplete (like templates) and creation of an object is completely meaningless here. If we want to do something like that, we must create an instance of class / type that implements that interface. For Example :- ILIST(T) interface provided in System.Collections.Generic namespace showing the below error while i was trying the same.


As i told you before, we must have to use it as a variable which points to a class which implements that interface to workaround this kind of problem. Something like that:-


Here, List(T) is a class which implementing ILIST(T) interface. Please remember that, ILIST(T) and ILIST are not same, the only difference is that first one is generic in type and we can pass any type in place of T. 

That’s it!!…..Happy Programming...




Saturday, 31 January 2015

Why SQL Server allows NULLs for the BIT datatype

During the interview my friend was asked, why SQL Server allows NULLs for the BIT datatype.
I like to explain things with analogies and this is no different. Consider a table holding job applicants details for a Job Board Website. Something Like this:-


CREATE TABLE dbo.tblJobApplicants (
  CandidateID int,
  JobID int,
  DateApplied date,
  isApproved bit
)

-- Applicant is approved
INSERT INTO dbo.tblJobApplicants
  SELECT
    1,
    1,
    GETDATE(),
    1

-- Applicant is rejected
INSERT INTO dbo.tblJobApplicants
  SELECT
    2,
    1,
    GETDATE(),
    0

-- Applicant is in progress means just applied
INSERT INTO dbo.tblJobApplicants
  SELECT
    3,
    1,
    GETDATE(),
    NULL
The key field to note here is of course the BIT field which indicates the approval or disapproval of the applicant. Obviously, when a candidate applies to a job, the status of the applicant isn't known  means the applicant has not been accepted, nor rejected. I think, it is the only end of the application in which this field can make a meaningful value. Hopefully, this example helps explain just when you might require a NULL bit field.

That’s it!!…..Happy Programming...

Saturday, 17 January 2015

How to compare two dates in ASP.NET using JAVASCRIPT

In this article, i am going to show how to validate two dates in asp.net using JavaScript. It also make sure that difference between two dates should not be more than n number of days. In my case, i specified 7 but you can specify it according to your need.

function ValidateDate() {
                var startDate = document.getElementById("<%=txtDatefrom.ClientID%>");
                var endDate = document.getElementById("<%=txtDateTo.ClientID%>");
                //Get 1 day in milliseconds
                var oneDay = 1000 * 60 * 60 * 24;
                
                // to make sure end date is greater than or equal to start date
                if ((Date.parse(startDate.value) > Date.parse(endDate.value))) {
                    $(endDate).css('border', '1px solid red');
                    return false;
                }

                else {
                    // to make sure difference between two dates should not be more than 7 days
                    if (Math.round((Date.parse(endDate.value) - Date.parse(startDate.value)) / oneDay) > 7) {
                        $(endDate).css('border', '1px solid red');
                        return false;
                    }
                    $(endDate).css('border', '1px solid #d3cfc7');
                    return true;
                }
            }