Monday, October 8, 2012

C# 4.0 : Named arguments and Optional parameter


Along with Optional Parameter C# 4.0 introduces a new feature of Named parameters as well, where we can provide a value of parameters with their name. We can pass arguments in any order, there is no need to remember the order of parameters.

Example1: Named Parameters
public void Hello(string strGreeting, string strWin)
{
     Console.WriteLine(strGreeting + “ ” + strWin);
}

Irrespective of the order of parameters, output of both the method calls will be same here.

Hello(strGreeting: "Hello!", strWin: "India");
Hello(strWin: "India", strGreeting: "Hello!");
Hello("Hello!", strWin: "India");

Output: 
Hello! India
Hello! India


Example2: Named and Optional

Named arguments are generally used with optional parameters.

public void Hello(string strGreeting = "Hello!", string strWin= "Winshuttle")
{
       Console.WriteLine(strGreeting + " " + strWin);
}

Hello();
Hello("Hello!");
Hello("Hello!", "Winshuttle");
Hello(strGreeting: "Hello!", strWin: "Winshuttle");
Hello(strWin: "Winshuttle", strGreeting: "Hello!");
Hello("Hello!", strWin: "Winshuttle");

Output: 
Hello! Winshuttle
Hello! Winshuttle
Hello! Winshuttle
Hello! Winshuttle
Hello! Winshuttle

Note:
  • Named argument must come after all positional parameters.
  • These both are useful when we call COM API’s.
  • We can omit method overloads using these features.

C# 4.0 : Optional Parameters


C# 4.0 has introduced a new feature of optional parameters, where we can provide a default value of a parameter in its declaration itself. Default parameters can be applied to methods, constructors, indexers or delegate.

Example1:
public void Hello(string strGreeting = "Hello!")
{
     Console.WriteLine(strGreeting);
}

Here pstrGreeting is an optional parameter and can be omitted during method call.

Hello();
Output:  Hello!

If we do not supply value of pstrGreeting, it will be default to “Hello!”

Example2:

void Hello (string pstrGreeting = “Hello!”, string strWin = “Winshuttle”)
{
                Console.WriteLine(pstrGreeting + “ “ + strWin);
}

Outputs for different versions are as follows:
Hello();                                  // Hello! Winshuttle
Hello("HELLO");                  // HELLO Winshuttle
Hello("HELLO", "INDIA");   // HELLO INDIA

Note:
  • If we add a default parameter to a public method then any depended assembly will also required recompilation.
  • Default value must be specified by a constant expression.
  • Optional parameter can’ be ref or out.
  • Mandatory parameters must come before optional except params (always come last).
  • String.Empty cannot be used as default value as it is not a compile-time constant. It is a static property defined in the string class.


Thursday, January 19, 2012

Cannot view XML input using XSL style sheet. Access is denied


Problem:

If you see following error while rendering XML file in IE.












Try following steps to overcome this problem.


Solutions:
Unblock access to individual files by the following:
  1. Open in Windows Explorer the directory containing the file, either the XML file or one referenced by the XML.
  2. Right click on the file and open Properties.
  3. Click Unblock.
Reason:
The following error may occur when accessing XML files downloaded from one computer to another such as the ZIP files for the assignment. Vista (and perhaps XP) blocks access to files, presumably attempting to protect you from using files you have copied to your computer.

Monday, May 30, 2011

Flicker free controls: SplitContainer

.Net Flicker Fee Controls 


Here are the steps to use this control:
1. Add new class "NonFlickerSplitContainer" to your C# application.
2. Replace auto generated class code with C# code shown below.
3. Use NonFlickerSplitContainer object instead of SplitContainer object in your application.

    public partial class NonFlickerSplitContainer : SplitContainer
    {
        public NonFlickerSplitContainer()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                               ControlStyles.UserPaint |
                               ControlStyles.OptimizedDoubleBuffer, true);

            MethodInfo objMethodInfo = typeof(Control).GetMethod(             "SetStyle",BindingFlags.NonPublic|BindingFlags.Instance);

            object[] objArgs = new object[] { ControlStyles.AllPaintingInWmPaint |
                                                                ControlStyles.UserPaint |
                                                               ControlStyles.OptimizedDoubleBuffer, true };

            objMethodInfo.Invoke(this.Panel1, objArgs);
            objMethodInfo.Invoke(this.Panel2, objArgs);
        }
    }

Flicker free controls: ListView

.Net Flicker Fee Controls 


Here are the steps to use this control:
1. Add new class "NonFlickerListView" to your C# application.
2. Replace auto generated class code with C# code shown below.
3. Use NonFlickerListView object instead of ListView object in your application.
   
public partial class NonFlickerListView : ListView
    {
        public NonFlickerListView()
        {
            this.GetType().GetProperty("DoubleBuffered",
System.Reflection.BindingFlags.NonPublic |                                                      System.Reflection.BindingFlags.Instance
).SetValue(this, true, null);
        }

            protected override void WndProc(ref Message messg)
        {
            if ((int)0x0014 == messg.Msg) // if message is is erase background
            {
                messg.Msg = (int)0x0000; // reset message to null
                return;
            }
            base.WndProc(ref messg);

        }
    }

Friday, May 27, 2011

Flicker free controls: GroupBox

.Net Flicker Fee Controls

Here are the steps to use this control:
1. Add new class "NonFlickerGroupBox" to your C# application.
2. Replace autogenerated class code with C# code shown below.
3. Use NonFlickerGroupBox object instead of GroupBox object in your application.

public partial class NonFlickerGroupBox : GroupBox
{
        public NonFlickerGroupBox() : base()
       {
                   this.SetStyle(ControlStyles.AllPaintingInWmPaint,
                                       ControlStyles.UserPaint
                                       ControlStyles.OptimizedDoubleBuffer,
                                       true);
       }
}

Fell free to post any query.

Flicker free controls: Panel

.Net Flicker Fee Controls
In first post Iam gonna post code of Filcker free Panel control. You can use this control in your application.

Here are the steps to use this control:
1. Add new class "NonFlickerPanel" to your C# application.
2. Replace autogenerated class code with C# code shown below.
3. Use NonFlickerPanel object instead of Panel object in your application.

public partial class NonFlickerPanel : Panel
{
       public NonFlickerPanel() : base()
       {
              this.SetStyle(ControlStyles.AllPaintingInWmPaint,
                                  ControlStyles.UserPaint
                                  ControlStyles.OptimizedDoubleBuffer,
                                  true);
       }
}
 
Fell free to post any query.