Friday, November 26, 2010

Countdown timer using Javascript

To display the countdown timer using javascript on a webpage like:

28 days, 17 hours, 43 minutes, 58 seconds

Please add the following code in the head section of the webpage:


Please add the following code in the body section of the webpage:

Monday, November 15, 2010

Implement Factory Design Pattern using C#.Net

Create an application with four classes. Three of the classes should contain data and behavior characteristics for circle, rectangle, and cylinder. the fourth class should allow the user to input a figure type from a menu of options. prompt for appropiate values based on the inputted figure type, instantiate an object of the type entered, and display characteristics about the object.
The main class should work with the other classes (circle, rectangle, and cylinder)
To achieve this scenario it is required to implement the factory design pattern.

    class Program
    {
        static void Main(string[] args)
        {
            int shapeType = GetShapeType();

            if (shapeType>3)
            {
                
                Console.WriteLine("Invalid shape type. Please select either 1, 2 or 3");
                shapeType = GetShapeType();
            }

            IShape objShape;
            objShape = ShapeFactory.GetShape(shapeType);
            objShape.DisplayShapeDetails();
            Console.ReadKey();
        }

        private static int GetShapeType() 
        {
            Console.Clear();
            int shapeType;
            Console.Write("Please enter the type of Shape:");
            shapeType = Convert.ToInt16(Console.ReadLine());
            return shapeType;
        }
    }

This enum will generalize the implementation for various classes required related to other different shapes:

    public enum Shape
    { 
        Circle=1,
        Rectangle=2,
        Cylinder=3
    }

This interface will be used for defining the contract for various shapes

    public interface IShape 
    {
        void DisplayShapeDetails();
    }

As per the requirement specified ShapeFactory is the fourth class which is responsible for centralized creation of the object of type of class of the shape specified by the user.

    public class ShapeFactory
    {
        public static IShape GetShape(int _shapeType) 
        {
            IShape objShape;

            switch (_shapeType)
            {
                case ((int)Shape.Circle):
                    int radius;
                    Console.Write("Please enter the Radius of the circle:");
                    radius = Convert.ToInt16(Console.ReadLine());
                    objShape= new Circle(radius);
                    break;

                case ((int)Shape.Rectangle):
                    int length, width;
                    Console.Write("Please enter the Length of the Rectangle:");
                    length = Convert.ToInt16(Console.ReadLine());
                    Console.Write("Please enter the Width of the Rectangle:");
                    width = Convert.ToInt16(Console.ReadLine());
                    objShape = new Rectangle(length, width);
                    break;

                case ((int)Shape.Cylinder):
                    int height;
                    Console.Write("Please enter the Height of the Cylinder:");
                    height = Convert.ToInt16(Console.ReadLine());
                    Console.Write("Please enter the Radius of the Cylinder:");
                    radius = Convert.ToInt16(Console.ReadLine());
                    objShape = new Cylinder(height, radius);
                    break;

                default:
                    objShape = null;
                    break;
            }
            return  objShape;
        }
    }

Circle class

    public class Circle  : IShape
    {
        private Shape _shapeType;
        private double _radius;

        public Circle() { }

        public Circle(double Radius) 
        {
            _radius = Radius;
            _shapeType = Shape.Circle;
        }

        public void DisplayShapeDetails()
        {
            Shape test = (Shape)_shapeType;
            Console.WriteLine("Area of the selected shape '{0}' is {1}", Enum.GetName(typeof(Shape), _shapeType), CalculateArea().ToString());
        }

        private double CalculateArea() 
        {
            return Math.PI * (_radius * _radius);
        }
    }

Rectangle Class

    public class Rectangle : IShape
    {
        private Shape _shapeType;
        private double _length;
        private double _width;

        public Rectangle() { }

        public Rectangle(double Length, double Width)
        {
            _length = Length;
            _width = Width;
            _shapeType = Shape.Rectangle;
        }

        public void DisplayShapeDetails()
        {
            Shape test = (Shape)_shapeType;
            Console.WriteLine("Area of the selected shape '{0}' is {1}", Enum.GetName(typeof(Shape), _shapeType), CalculateArea().ToString());
        }

        private double CalculateArea()
        {
            return _length * _width;
        }
    }

Cylinder class

    public class Cylinder : IShape
    {
        private Shape _shapeType;
        private double _height;
        private double _radius;

        public Cylinder() { }

        public Cylinder(double Height, double Radius)
        {
            _height = Height;
            _radius = Radius;
            _shapeType = Shape.Cylinder;
        }

        public void DisplayShapeDetails()
        {
            Shape test = (Shape)_shapeType;
            Console.WriteLine("Area of the selected shape '{0}' is {1}", Enum.GetName(typeof(Shape), _shapeType), CalculateVolume().ToString());
        }

        private double CalculateVolume()
        {
            return (Math.PI * _height * _radius * _radius);
        }
    }

If it is required to add another shape (For example Cube) then you have define a corresponding class named 'Cube' which implements the 'IShape' interface and need to add a case for the same into the ShapeFactory class.

Monday, November 1, 2010

What are implicitly typed variables in C#.Net 2008

Using C# 2008, one can now declare a variable and allow the compiler to determine the type of the item implicitly.

LINQ uses this capability to work with the queries that are created.

To work with this new capability, the 'var' keyword is being used as:

var x = 100;

When you use this statement, the compiler will actually use the value of 5 to figure out the type that this needs to be. That means, in this case, that the statement will actually be as you would expect:

int x = 100;

Friday, October 29, 2010

How to check SQL Table exists or not in SQL Server

CREATE PROCEDURE CheckIfTableExists(@TableName
)
AS NVARCHAR(100)AS
BEGIN
SET
DECLARE
NOCOUNT ON @TableId as VARCHAR(50)SELECT @TableId= OBJECT_ID(N'[dbo].' + @TableName + '')IF (@TableId IS NOT NULL)SELECT 'Table Exists'ELSESELECT 'Table do not exists'END

How to check:
EXEC CheckIfTableExists 'stores'

Thursday, October 28, 2010

How to call same method avaialble in two different interfaces and implemented in single class

I have the following two interfaces IDemo1 and IDemo2, which have the methods which same names PrintHello. DemoClass has implemented both these interfaces and has provided a separate implementation of both these methods. Now from main method, I want to call method of the appropriate interfaces:

 Output:


 
       

Difference between INTERSECT and EXCEPT in SQL Server






Monday, August 16, 2010

Do you know?

All WCF services expose Contracts. The contract is never platform-specific and contracts describes what the service actually does. WCF defines four types of contracts:


[1] Service contracts - Describe which operations the client can perform on the service.
[2] Data contracts - Define which data types are passed to web service and which data tyes retrieved from the service.
[3] Fault contracts - Define which errors are raised by the service, and how the service handles the errors to its calling clients.
[4] Message contracts - Allow the services to interact directly with messages.