Thursday, December 1, 2011

Delegates in C#

Delegates is a type that reference Method. When it assigned by a method it behaves exactly same as that method. It can be used as parameters and can be returned a value. So it has same what are the methods have.
Delegates makes is possible to programmatically change method calls, and plug new codes in to existing classes. For these you need to know what are the method matches delegate’s signature and its return type. Delegates are similar to C++ function pointers but delegates type safe and Object Oriented. Methods don’t need to match the delegate signature exactly.
What is Covariance and Contravariance in Delegates ?
Covariance and Contravariance provides kind of flexibility when matching the method signature with delegate type that is defined.
Covariance permits a method to have a more derived return type than what is defined when the delegate define. Contravariance permits a method to have less derived parameter types than in the delegate type.

A delegate is a type it encapsulates a method see below example:
        // Define the Delegate
        public delegate void TestDelegate(string Message);

        // Creating a method with string argument
        public static void DisplayMessage(string Message)
        {
            Console.WriteLine(Message);
            Console.ReadKey(); 
        }

        static void Main(string[] args)
        {
            // Instantiate the delegate and encapsulates the method "DisplayMessage"
            TestDelegate Test = DisplayMessage;

            // call the delegate
            Test("This is my delegate method");
        }

 This is very simple example but delegate can used in advance way rather this. The instantiated delegate is an object, it can be passed as parameter, or assign to a property. When a delegate is use in this way the code using by the delegate does not need any knowledge of the method being used. This is similar functionality provide by interface encapsulation.
What is Multicasting?
In a delegate you can have more than one method to invoke, you can assign method to a delegate using “=” and you can add method to a delegate where it has assigned a method to invoke by using “+” if you want you can remove assigned method from a delegate where it has more methods to invoke by using “-“. This is called as multicasting.

Multicasting example:

namespace Delegates_Test2
{
    delegate void Del();


    class Simple
    {
        public void MethodA()
        {
            System.Console.WriteLine("Message from the method A.");
        }

        static public void MethodB()
        {
            System.Console.WriteLine("Message from the method B.");
        }

    }
}

 namespace Delegates_Test2
{
    class Program
    {
        static void Main(string[] args)
        {
            Simple sc = new Simple();

            // Map delegate to the method: A
            Del d = sc.MethodA;
         

            // Map method: B with multicasting
            d += Simple.MethodB;
           
            d(); // Both methods are excute here

            Console.ReadKey(); 

        }
    }
}


Generic Delegates
The delegate can define its own generic type parameters.
How to define:

public delegate void MyDelegate<T>(T item);
Generic delegate example:
namespace Generic_Delegate
{
    //Generic Delegate define here with return value of T, and both T arguments
    public delegate T Calculator<T> (T arg1, T arg2);

    class MathUtility
    {
    
    // Generic method to execute with Two generic argument values and one generic  methods  
        public static void Calculate<T>(T[] values1, T[] values2, Calculator<T> t)
        {
            for (int i=0; i < values1.Length; i++)
            {   // execute the T method with two of T arguments
                values1[i] = t(values1[i],values2[i]);
                //And assigning results to First parameter
     
            }
        }
    }
}



namespace Generic_Delegate
{
    class Program
    {
        // My method to find  square value
        static int FindSquareValue(int x, int y)
        {
            // Your method's implementation
            return (x * y); // Your method have defined to return the results
        }

        // My Method to find dividence with first value by second value
        static double FindDividence(double a, double b)
        {
            // Your method's implementation
            return (a / b);  // Your method have defined to return the results
        }
          
        static void Main(string[] args)
        {
            // To get Square
            int[] height = { 10, 5, 8 };
            int[] width = { 4, 3, 6 };

            MathUtility.Calculate(height, width, FindSquareValue); // Dynamically hook with
                                                                   // FindSquare method
            // You're only passing the both values and method
          

            Console.WriteLine("Display Square Values..."); 
           
            foreach (int i in height)
            {
                Console.WriteLine(i.ToString());   
            }
    

           // To get Dividence
            double[] Val1 = { 10, 80, 60 };
            double[] val2 = {8,3,4};

            MathUtility.Calculate(Val1, val2, FindDividence);

            Console.WriteLine("Display Dividence values..."); 

            foreach (double d in Val1)
            {
                Console.WriteLine(d.ToString());  
            }

            Console.ReadKey(); 
        }
    }
}


Hope this documentation may valuable for you!!

Wednesday, November 16, 2011

how to reboot a stuck iPhone ( iPhone 4S, iPhone 4, iPhone 3GS, iPhone 3G, Original iPhone)


Instructions on how to reboot a stuck iPhone
               
(This process can apply for iPhone 4S, iPhone 4, iPhone 3GS, iPhone 3G, Original iPhone)


iOS 5 is facing problem with updating after been updated to iOS 5, iPhone is stuck in recovery mode (Apple logo & black screen repeat repeatedly).

You can reset it in normal way of how when the iPhone crashes or locks up that prevents the iPhone from responding to being turned off.

Step 1.
Reset the iPhone by holding the sleep/wake button at the top right of the device and the home button at the bottom center of the face at the same time.
 
Step 2.
Hold them both until you see the screen go black. (if you have connected to any PC remove the connection first).

Step 3
Wait until the white Apple logo appears.

Step 4. 
When this happens, you can let go - the iPhone is rebooting.

An iPhone reset will not normally remove or delete any data or settings. However, the term reset is also sometimes used to mean deleting all content from it.

Any way you can start the iPhone with factor settings as new iPhone with connecting to iTunes. But if you have made your iPhone iCloud back up or iTunes backup of your Phone simply you can restore it.

Monday, November 14, 2011

Crystal report 2008 Redistribution Package


I faced a problem when I’m deploying new application developed with Visual Studio 2010 and Crystal Report 2008.

Error Message: 

Could not load file or assembly 'CrystalDecisions.CrystalReports.Engine, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' or one of its dependencies. The system cannot find the file specified.

But I have installed Crystal report 2008 on client’s PC. 

Solution,

There is a redistribution package used to install. It doesn’t require installing Crystal report 2008 and this redistribution package is free of license.

Follow these links to download the packages.

For 32 bit

For 64 bit
http://downloads.businessobjects.com/akdlm/cr4vs2010/CRforVS_redist_install_64bit_13_0.zip

Thursday, October 6, 2011

Transactions in ADO.Net ( TransactionScope Class )


A transaction is a logical unit of a work. There are few ways in implementing transactions.  Either you can implement it in your T-SQL Statements or inside of the program logic. There are two ways in implementing in the .net programming code you can use DbTransaction Object or TransactionScope.
I would implement here TransactionScope class.
It creates a standard transaction called a “local lightweight transaction” that is automatically promoted to a full-fledged distributed transaction if required. You can use even multiple connection objects if you have to update tables in two databases.
First have to import System.Transactions namespace.

        public void CreateTransactionScope()
        {
            SqlCommand ObjCmd = new SqlCommand();
            try
            {

                  // TransactionScope object defined here
                using (TransactionScope Scope = new TransactionScope()) 
                {
                    // _ConnectionString is the Class globally defined string value for the
                    database connection string
                    using (SqlConnection Sqlcon = new SqlConnection(_ConnectionString))
                    {
                        Sqlcon.Open(); // Opening the connection

                        // Assign Connection Object to SQLCommand Object
                        ObjCmd.Connection = Sqlcon;

                        //SQL Statement  to execute
                        ObjCmd.CommandText = "INSERT INTO TBL_EMOPLOYEES VALUES('"+ "001"
                                       +"', '"+ "Pathum Tiranga" +"','"+ "01/01/2011" +"')";
                        ObjCmd.CommandType = CommandType.Text;   

                        //Execution happen here
                        ObjCmd.ExecuteNonQuery();
                    }

                    /* At the ending of the Scope of the Transaction Scope commiting the
                      transaction was done 

                     If couldn't reach this statement all above datachanges happened to one
                      or more tables will be RolledBack
                     */
                    Scope.Complete();
                }
            }

            /* If an exception is thrown within the TransactionScope object’s using block,
               the transaction aborts, and all work is rolled back. */
            catch (Exception ex)
            {
                throw ex;
            }
        }

When you create a TransactionScope object in a using block, the TransactionScope object assigns a transaction to this created connection, so you don’t need to add anything to your code to enlist this connection into the transaction.

Mapping data flows in Azure Data Factory

  Overview: Data flows are one of the features inside the Azure Data Factory which allows data engineers to develop data transformation logi...