Friday, April 30, 2010

C# 4 Optional Parameters and COM no ref

Pro .NET 4 Parallel Programming in C#COM had the concept of Optional Parameters. For example the COM TreeView control's Nodes.Add method expects six parameters. The last two parameters and Image and Selected Image.

Working with such controls from C# requires embedding a lot of Type.Missing statements which makes the code bulky as described in the latter part of my blog Using COM Controls in .Net

In that blog I have demonstrated working with COM controls from both VB .Net and C#.

With the introduction of Optional Parameters in C# 4 you can call COM Components by reducing unwanted tweaking.

COM no ref

COM Components also expect certain parameters to be of pass by ref and you had to explicitly mention the ref keyword in the previous version of C#. C# 4 relaxes the code by allowing you to omit the ref modified.

Here is the C# 4 code

private void Form1_Load(object sender, EventArgs e)
{
mscomctl.Node Nodx;
Nodx = axTreeView1.Nodes.Add(Type.Missing, Type.Missing, "India", "India");
Nodx = axTreeView1.Nodes.Add("India", mscomctl.TreeRelationshipConstants.tvwChild, "Kerala", "Kerala");
Nodx.EnsureVisible();

axTreeView1.Nodes.Add(Nodx);
}



No comments:

Post a Comment