Search This Blog

Wednesday, October 23, 2013

Mulicast delegate in C#

C# > Delegate

Mulicast delegate is a delegate that can have more than one element in its invocation list.

Example

public delegate void MultiDelegate(int x,int y);
private class Operator
{
            public void Add(int x, int y)
       {
              MessageBox.Show ("The sum is: " + (x + y));
       }
       public void Sub(int x, int y)
       {
              MessageBox.Show("The sub is: " + (x - y));
       }
}

MultiDelegate ptr=null;
//add more than one function called multicast delegate.
ptr += new MultiDelegate(obj.Add);
ptr += new MultiDelegate(obj.Sub);
ptr(50, 10);

<< Delegates                                                                                  Anonymous functions >>





Delegates in c#

C# > Delegate

A delegate in C# is similar to a function pointer in C++. This allows encapsulating a reference to a method inside a delegate object.
A delegate is a type that defines a method signature. When you instantiate a delegate, you can associate its instance with any method with a compatible signature. You can invoke (or call) the method through the delegate instance.

An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references
Delegates are especially used for implementing events and the call-back methods.

1. Basic delegate example

// declaration

        private delegate void TestDelegate();

        private  void StartTestDelegate()
        {
            MessageBox.Show("StartTestDelegate"); 
        }
        private void StopTestDelegate()
        {
            MessageBox.Show("StopTestDelegate");
        }

// Instantiation
        TestDelegate del1 = new TestDelegate(StartTestDelegate);
        TestDelegate del2 = new TestDelegate(StopTestDelegate);

// Invocation
        del1();
        del2();

2. Math Operator delegate example

  private class Math
  {
            public int Add(int x, int y)
            {
                return x + y;
            }
            public int Sub(int x, int y)
            {
                return x - y;
            }
            public MathDelegate Operator(int op) 
            {
                MathDelegate objDelegateMath = null;  // delegate reference
                if (op == 1)
                    objDelegateMath = Add;  // point the reference to methods
                else
                    objDelegateMath = Sub;  // point the reference to methods
               return objDelegateMath; 
            }
  }

  Math objMath = new Math();
  //Invoke the methods through delegate
  MessageBox.Show(objMath.Operator(1).Invoke(1, 2).ToString());
  MessageBox.Show(objMath.Operator(2).Invoke(5, 4).ToString()); 

3. Logger class delegate example 

Multicast delegates >>






Tuesday, October 22, 2013

Switch statement C#

C# > Statements > switch

Switch selects a switch section to execute from a list of values

Switch expression: numeric, char, enum or string.
Every statement sequence in a case must be terminated with:
  • break 
  • return
  • goto
  • throw
If no case label matches default.
If no default specified continuation after the switch statement.

Example:

  private void TestSwitch(int a)
        {
            int b = 0;
            switch (a)
            {
                case 1:
                    b += 1;
                    break;
                case 2:
                    b += 1;
                    goto case 1;
                case 3:
                    return;
                default:
                   MessageBox.Show("Invalid selection.");
                   break;
            }
        }






Boxing and Unboxing C#


Boxing is the process of converting a value type to the type object. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap.
Unboxing extracts the value type from the object.

Example:

int i = 10;
object o = i; // boxes i
i = (int)o;   // unboxing





SQL Statements - DML, DDL,DCL

SQL Server > SQL Statements

SQL Statements are used for querying with database

Data Manipulation Language (DML)

Data Definition Language (DDL)

Data Control Language (DCL)
  • Grants or remove rights to database and structures within it






Monday, October 21, 2013

Run exe from SQL Server

SQL ServerSystem Stored Procedures > xp_cmdshell

xp_cmdshell is an extended stored procedure provided by Microsoft and stored in the master database.
This procedure allows you to issue operating system commands directly to the Windows command shell via T-SQL code.

Example: How to run executable from SQL Server?

exec master.dbo.xp_cmdshell 'mkdir "c:\MyDir\"'









Thursday, October 17, 2013

Class and object C#

C# > OOP > Class and object


A class is an abstraction of the characteristics and behavior of an object type. Classes can inherit from one other class, but can implement multiple interfaces.
A class is a template for defining objects.
A class should not be confused with an object.
An object is an instance of a class.




Difference between class and object

Class is like a template or blueprint of the object and resides on hard disk.
Object is real world implementation of a class and resides on RAM.

Example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class Person // define Person class
    {
        public string Name { get; set; } // Name property
    }

    public partial class Form1 : Form
    {
        private void Form1_Load(object sender, EventArgs e)
        {
            Person p1 = new Person(); // create new Person object
            p1.Name = "Dan";
        }
        public Form1()
        {
            InitializeComponent();
        }
     }
}