Tuesday, February 26, 2013

Select Column Comma Separated in SQL Server

-Example 1:

SELECT top 1
              STUFF ( ( SELECT ','+inTab.FIRST_NAME
                           FROM  crm.CONSUMERS inTab
                              FOR XML PATH(''),TYPE).value('.','VARCHAR(MAX)'),1 ,1,SPACE(0)) as ConsumersListWithComms
FROM crm.CONSUMERS OutTab


--Example 2:

SELECT TOP 1 (SELECT inTab.FIRST_NAME+','
                     FROM CRM.CONSUMERS inTab FOR XML PATH(''))
                     AS ConsumersListWithComms
FROM CRM.CONSUMERS outTab

Monday, February 25, 2013

Constant key:
1.      Const is by default static but cannot mark as static.
For exp:   public static const int _strConst = 10; //error
                 //constant cannot mark static.
 public const int _strConst = 10; // good

    public class classConst
    {
        public const int _strConst = 1;

        static void Main(string[] args)
        {}
     }

2. Const key values must be initializing at the declaration time only.
3. Const key values is initializing at compile time.
4.  Const Key values cannot change at the Run time.
                       
Readonly key:
1.      In Readonly key marked as static.
            For exp:   public static readonly int _strReadOnly ;// good
            public readonly int _strReadOnly ;// good
            public readonly int _strReadOnly=10 ;// good

    public class ClassReadOnly
    {       
        public static readonly int _strReadOnly ;
        public ConstReadOnly()
        {
             _strReadOnly = 10;
        }
        static void Main(string[] args)
        {}
     }
2.      Readonly key is initializing maybe at the declaration time, in the constructor, in the web config.
3.      Readonly values is initializing at Run time.
4.      Readonly values can be changed at the Run time.

Friday, February 15, 2013

Difference between ref and out keyword



Example: ref keyword
 
    Initialization not must to inside in method.
    Before calling a method must be Initialized the values of variables.

namespace ConsoleApplication1
{
    public class swapClass 
    {
        // Swap using third variable, ref keywords
        public  void swapMethod(ref int a, ref int b)
        {
            // Initialization not must to inside in method.
            // before calling a method must be Initialized the values of variables.
         
            int Temp;
            Temp = a;
            a = b;
            b = Temp;
        }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            swapClass obj = new swapClass();
            // using ref Keywords
            int a=10;
            int b=20;         
            obj.swapMethod(ref a, ref b);
            Console.WriteLine("After Swap A= " + a + " B =" + b);

            Console.ReadLine();
        }
    }
}





Example: Out keyword

     Initialization must to inside in method
     Before calling a method maybe initialized or not the values of variables.

namespace ConsoleApplication1
{
    public class swapClass 
    {
        // Swap without using third variable, out keywords
        public void swapWTMethod(out int c, out int d)
        {
          // Initialization must to inside in method
          // before calling a method maybe Initialized or not the values of variables.
            c = 10;
            d = 20;
            c = c + d;
            d = c - d;
            c = c - d;         
        }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            // using out keywords
            int c;
            int d;
            obj.swapWTMethod(out c, out d);
            Console.WriteLine("Before SwapWT C= " + c + " D =" + d);           

            Console.ReadLine();
        }
    }
}


Thursday, February 14, 2013

swap two values without third variable



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class swapClass 
    {
        // Swap without using third variable
        public void swapWTMethod(ref int a, ref int b)
        {
            a = a + b;
            b = a - b;
            a = a - b;         
        }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            swapClass obj = new swapClass();
            int a=10;
            int b=20;
            Console.WriteLine("Before SwapWT A= " + a + " B =" + b);
            obj.swapWTMethod(ref a, ref b);
            Console.WriteLine("After SwapWT A= " + a + " B =" + b);

            Console.ReadLine();
        }
    }
}