Tuesday, February 10, 2015

Clean way to maintain Session in a single class in ASP.Net application

We can use a wrapper class around the ASP.NET session to maintain session in a single class in ASP.Net application. This class stores one instance of itself in the ASP.NET session and allows you to access your session properties in a type-safe way from any class. Check the below code,
public sealed class Sessions : System.Web.UI.Page
{
     private const string SESSION_MANAGER = "PROJECTNAME_SESSION_OBJECT";

     private Sessions()
     {
          //You can initialize any variables here.
     }

     public static Sessions Current
     {
          get
          {
               Sessions session = (Sessions)HttpContext.Current.Session[SESSION_MANAGER];
               if (session == null)
               {
                     session = new Sessions();
                     HttpContext.Current.Session[SESSION_MANAGER] = session;
               }

               return session;
          }
     }

     // properties
     public string Username{ get; set; }
     public DateTime LoggedinDtae { get; set; }
     public int UserId { get; set; }
}
And you can access those session properties like below,
     //to get UserId
     int userId= Sessions.Current.UserId;
     //to set UserId
     Sessions.Current.UserId = 101;

     //to get Username
     string username= Sessions.Current.Username;
     //to set Username
     Sessions.Current.Username = "test name";

     //to get LoggedinDate
     DateTime loggedinDate = Sessions.Current.LoggedinDate;
     //to set LoggedinDate
     Sessions.Current.LoggedinDate= DateTime.Now;

Wednesday, September 17, 2014

Project Euler Solution using C#: Problem 28 : Number Spiral Diagonals

Problem:

Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13

It can be verified that the sum of the numbers on the diagonals is 101.

What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way? My Solution:

static void Main(string[] args)
{
    static void Main(string[] args)
    {
        int limit = 1001 * 1001;
        int count = 0;
        int loopCount = 2;

        int sum = 1;   //1 coz the centervalue is 1. In loop counting from 3. 
        // every 4 iteration the increment count is adding +2
        for (int i = 3; i <= limit; i += loopCount)
        {
            sum += i;
            count += 1;
            if (count >= 4)
            {
                loopCount += 2;
                count = 0;
            }
        }
        Console.WriteLine(sum);
        Console.ReadLine();
    }
}

Note: You can simplifies the coding :)

Project Euler Solution using C#: Problem 25: 1000 Digit Fibonacci Number

Problem:

The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
Hence the first 12 terms will be:

F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144

The 12th term, F12, is the first term to contain three digits.
What is the first term in the Fibonacci sequence to contain 1000 digits? My Solution:

static void Main(string[] args)
{
    int count = 3;
 Dictionary F = new Dictionary();
 F.Add(0, 0);
 F.Add(1, 1);
 F.Add(2, 1);
 while (true)
 {
  BigInteger index1 = count - 1;
  BigInteger index2 = count - 2;
  BigInteger val = F[index1] + F[index2];
  F.Add(count, val);
  if (val.ToString().Length >= 1000)
  {
   break;
  }
  count += 1;
 }
 Console.WriteLine(count);
 Console.ReadLine();
}

Note: You can simplifies the coding :)