Friday 20 December 2013

Add Rule and Insert Rule methods Css stylesheet

Add Rule and Insert Rule methods :


 Sometimes we need to change our css rules dynamically. With Css-rules ,I mean to say style-definition of css classes and ids.
 We can add new rules to our predefined class using the insertRule and addRule (as reqd.) methods to add new rules to our css class 
 as is shown below:

 The addRule method accepts three arguments:  the selector, the second the CSS code for the rule, and the third is the zero-based integer 
 index representing the style position (in relation to styles of the same selector):

 sheet.addRule("#myId", "float: left; background: red !important;", 1);


 The CSSStyleSheet.insertRule() method inserts a new style rule into the current style sheet.

sheet.insertRule("#myId", "float: left; background: red !important;", 1);


function addCSSRule(sheet, selector, rules) {
if(sheet.insertRule) {
sheet.insertRule(selector + "{" + rules + "}", index);
}
else {
sheet.addRule(selector, rules, index);
}
//if -else is used because some browsers don't support insertRule and some don't support addRule
}
// we can use it as
addCSSRule(document.styleSheets[0], "header", "float: left");

Happy Coding!

Tuesday 17 December 2013

Use of delegates to improve the performance of application

Delegates:

Definintion:

Delegate is a type which  holds the method(s) reference in an object. It is also referred to as a type safe function pointer.

Using delegates basically includes three stages:

1) Defining Delegate.
2) Declaring Delegate.
3) Method Definition.

These stages are elaborated as follows:

1) Defining Delegate:

public delegate double AddNumbers(double num1, double num2);
While defining delegate, we are supposed to provide the parameters and the datatype of the delegate. In our example , the delegate has two parameters num1 and num2 which are of double type . And also the return type of our delegate is double.

2) Declaring Delegate:

Now after defining delegate, we need to declare our delegate .
public AddNumbers objAdd = null;
I have created this object of our delegate and instantiated it to null. So that I can use it anytime in my code file without declaring it again and again.

3) Method Definition:

Now the final thing that we need to do is to create the method that is going to be referenced by our delegate.

So here is our method :

public double AddMyValues(double val1, double val2)
{
    return val1+val2;
} 

Now, how we are going to integrate all these , is described below:

public delegate double Delegate_Add(double a,double b);
 class MainClass
 {
    static double AddMyValues(double val1, double val2)
    {
        return val1+val2;
    } 
    static void Main(string[] args){
    //Creating the Delegate Instance
    Delegate_Add delObj = new Delegate_Add(AddMyValues);
    Console.Write("Please Enter Values");
    double v1 = convert.toDouble(Console.ReadLine());
    double v2 = convert.toDouble(Console.ReadLine());
    double result = delObj(v1,v2); 
    Console.WriteLine ("Sum of two numbers is :"+result);
    Console.ReadLine(); 
  }
}
Advantages of using delegates :
1)Encapsulating the method's call.
2)Use of delegates improve the performance of application.
3)Used to call a method asynchronously.
Thus , by using delegates we can increase perfomance of our application.


Happy Coding!