Thursday 20 March 2014

Reference Types and Value Types

Reference Types:

variables store reference to a type.

we will see some scenarios :
scenario1:
        class student
        {
            public int id { get;set;}

        }
//'s1' is a variable of type 'student'
student s1 = new student();
//Assign value of s1 to s2 .(value of s1 is address of student object)
student s2 = s1;
s1.id = 23;
// since s1 and s2 are referring same student object, s1.id and s2.id contains same value.
Console.WriteLine("s1 " + s1.id + "  s2 " + s2.id ); 
o/p : s1 23  s2 23

scenario2:

//'s1' is a variable of type 'student'
student s1 = new student();
//Assign value of s1 to s2 .(value of s1 is address of student object)
student s2 = s1;
s1.id = 34;
//creating second instance of student object.
        s1 = new student();
        s1.id = 23;
// s2 is still referring to old student object, but s1 is now referring new student object, hence s1        and s2 holds 
different values
Console.WriteLine("s1 " + s1.id + "  s2 " + s2.id ); 
o/p : s1 23 s2 34

Value Types:

Variables holds value.
int x1 = 10;
//Assign value of x1 to x2(value of x1 is 4 not the address of 4)
int x2 = x1;
x1 = 23;
// since x2 holds value of x1, even if we initialize x1 value to 23 the value of x2 does not change to 23.
Console.WriteLine(x2);
o/p: 10

Wednesday 19 March 2014

Sample code which uses Delegates and Without using Delegates

You can think of a delegate type as being a bit like an interface with a single method. It specifies the 
signature of a method, and when you have a delegate instance, you can make a call to it as if it were a
method with the same signature. Delegates provide other features, but the ability to make calls with a
particular signature is the reason for the existence of the delegate concept.

we will see the implementation of calculating two numbers (Addition and Multiplication) using delegates and with out using delegates.


without using Delegate:


without using delegates, we need to create interface and implement that interface in two classes. after that we have to create objects for them and access them using object.methodname.


here is the code for it:

-------------------------------------------------------
namespace WithAndWithoutDelagates
{
    interface calculate1
    {
        int Eval(int a, int b);
       
    }
    class Add:calculate1
    {
        int calculate1.Eval(int a, int b)
        {
            return a + b;
        }
    }

    class Multiply : calculate1

    {
        int calculate1.Eval(int a, int b)
        {
            return a * b;
        }
    }
}

//////////////////////////////////////////////////////////////////////////////

 class Program
    {
static void Main(string[] args)
        {
           Console.WriteLine(DoWork1(new Multiply()));

            Console.WriteLine(DoWork1(new Add()));

            Console.ReadLine();
        }

        static int DoWork1(calculate1 f)

        {
            return f.Eval(20, 30);
         
        }
}

///////////////////////////////////////////////////////////////////////////

using delegates:

No interfaces, no clunky .eval stuff, no object instantiation, just simple function-pointer like usage, for a simple task.

-------------------------------------------------
 class Program
    {
        public delegate int calculate(int x, int y);

        static void Main(string[] args)

        {
            calculate del1 = new calculate(Add);
            calculate del2 = new calculate(Multiply);

   Console.WriteLine(DoWork(del1));

            Console.WriteLine(DoWork(del2));
            Console.ReadLine();
        }

        static int Add(int x, int y) 

        {
            return x+y;
        }

        static int Multiply(int x, int y)

        {
            return x*y;
        }

        static int DoWork(calculate f)

        {           
            var res = f(20, 30);
            return res;
        }

    }

Display a form containing check boxes,combo boxes on button click

Just Copy and Paste this Code..Works like Charm..!

Ext.require([
    'Ext.form.*',
    'Ext.window.Window'
]);

Ext.onReady(function() {
    // The data store containing the list of states
var states = Ext.create('Ext.data.Store', {
    fields: ['abbr', 'name'],
    data : [
        {"abbr":"AL", "name":"Alabama"},
        {"abbr":"AK", "name":"Alaska"},
        {"abbr":"AZ", "name":"Arizona"}
    ]
});

    Ext.create('Ext.Button', {
        text: 'Click',
        handler: function () {        
            Ext.create('Ext.form.Panel', {
        renderTo: Ext.getBody(),
        bodyStyle: 'padding: 5px 5px 0 5px;',
        defaults: {
            xtype: 'textfield',
            anchor: '15%',
         },
        items: [
{
                                xtype: 'checkbox',
                                boxLabel: 'Cricket',
                                name: 'sport',
                                checked: true,
                                inputValue: 'cricket'
                            },
                            {
xtype: 'checkbox',
                                boxLabel: 'TableTennis',
                                name: 'sport',
                                inputValue: 'tt'
                            },
                            {
xtype: 'checkbox',
                                boxLabel: 'Football',
                                name: 'sport',
                                inputValue: 'football'
                            },
{
xtype: 'textfield',
name: 'name',
fieldLabel: 'Name',
allowBlank: false  // requires a non-empty value
                            },
                            {
xtype: 'combo',
fieldLabel:'Division',
name:'division',
queryMode:'local',
store:['A','B','C'],
displayField:'division',
autoSelect:true,
forceSelection:true,
     
                            },
{
xtype: 'combo',
                                fieldLabel: 'Choose State',
store: states,
queryMode: 'local',
displayField: 'name',
valueField: 'abbr'
                            },
{
xtype: 'button',
                                text: 'Click Me',
handler: function() {
alert('Clicked!!');
this.up('form').getForm().submit();
}
                            }
   ]  
    });
        },
        renderTo: Ext.getBody()
    });
 
 
});