Saturday, November 21, 2009

.NET 4.0 and VS 2010 Series – Named and Optional Parameters

After my posts on the framework features over the past couple of weeks, I will now start focussing on the language aspects of C# 4.0. In C# 4.0, named and optional parameters have been introduced. This is one feature that I have been waiting for so I am only glad that the .NET team has decided to introduce this feature in C# 4.0.

VB 6.0 programmers are aware of the usage of the optional parameters. It used to be declared as

 Sub DoSomething(Optional ByVal Switch As Boolean = False)
What the above declaration meant was that you could call the DoSomething method without explicitly passing the Switch parameter. The default value of false will be assigned to the variable if it was not passed.

This was not possible in .NET. The workaround was to declare overloaded methods. You do not need to do that anymore. In C# 4.0, you can declare a method with an optional parameter simply by assigning a default value for the parameter in question like the example below.

private string Extract(String inputString, int endIndex=0, int startIndex =0)


Named parameters as the name suggests, enable the developers to skip a parameter while calling the method if they wish to. This is done by passing the parameter by quoting its name and value separated by a colon as shown in the syntax below

Extract(inputString, startIndex: 4);


The syntax above calls the method extract with three parameters the signature of which is described previously. You will notice that the second parameter endIndex has been skipped and the value is being passed for startIndex. Below is the complete code snippet


   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5: using System.Diagnostics.Contracts;
   6:  
   7:  
   8: namespace NamedOptionalParams
   9: {
  10:     class Program
  11:     {
  12:         static void Main(string[] args)
  13:         {
  14:             String inputString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  15:             String result = Extract(inputString,6);
  16:             String result1 = Extract(inputString, startIndex: 4);
  17:             String result2 = Extract(inputString, startIndex: 6, endIndex: 10);
  18:             Console.WriteLine(result);
  19:             Console.WriteLine(result1);
  20:             Console.WriteLine(result2);
  21:             Console.ReadLine();
  22:  
  23:         }
  24:  
  25:         //this method creates a substring based on the input parameters
  26:         //1. The input string is always passed
  27:         //2. Start Index is an optional parameter. If this is not passed then the 
  28:         //   start is assumed to be 0 which is the default value
  29:         //3. The End Index can be omitted. If it is omitted, then start Index has to
  30:         //   be passed and the string is from the start index to the end of the input
  31:         //   string
  32:         static string Extract(String inputString, int endIndex=0, int startIndex =0)
  33:         {
  34:  
  35:             if (endIndex==0)
  36:             {
  37:                 endIndex = inputString.Length;
  38:             }
  39:  
  40:             String outString = inputString;
  41:             outString = inputString.Substring(startIndex, (endIndex - startIndex));
  42:             
  43:             return outString;
  44:         }
  45:     }
  46: }

In the above example, the extract method just creates a substring for a specified start and end index. I have made three calls to the method. Note that with Named Parameters, the order in which you pass the parameters is not important as long as you don’t forget to name them correctly. You can see this in Line 17 of the above code.

That is it for this post on named and optional parameters. They are quite a useful feature that will reduce the number of overloaded methods that we will have to write. I am sure you will find more ways of putting them to use.

Tuesday, November 17, 2009

.NET 4.0 and VS 2010 Series - PIA – Rest in Peace

In previous versions of .NET, Primary Interop Assemblies (PIA) acted as the bridge between unmanaged COM components and managed code. Though PIA solved the problem of marshalling for almost all types, they came with their own problems. One of the biggest problem was that even if a small feature was used, the developer had to ship the complete PIA which is not a small payload by any means.

Also, a developer had to develop an assembly for each version of word or excel that the application targeted. This drastically increased the size and the complexity of the deployment package.

With .NET 4.0 this has changed. Developers no longer need to ship the whole PIA. .NET 4.0 gives the developer the flexibility of embedding only those features that are used by the application rather than shipping the whole assembly. This also means that the developer does not need to worry about the availability of the target applications like Word or Excel. Enabling embedded interop assemblies is very easy. You right click the interop reference and change the EmbedInteropTypes from false to True.

Enbed

When you do that, the interop assemblies are no longer referred. You can confirm this by using the reflector. The snapshot will look like below. Note that the Excel and Word are now namespaces within  the project instead of references.

Reflector

Friday, November 6, 2009

How to: Increase the size of your virtual hard disk

If you have run out of space in your virtual image hard disk and didn’t know the way out of it; read on. I ran into this problem last week and found the following solution after a bit of googling. Though there were a lot of posts targeted at VMWare, I was looking for ones specific to Microsoft VPC. The magic tool that you have to look at is called VHD Resizer. It is a free download available from the VM tool kit site. You just have to register on the site to download.

When you run the tool, it will ask you to point to the vhd whose hard disk size you want to increase. You can also set the size you want to increase it to. The only thing that needs to be remembered, is that it makes a copy of the VHD so you will need the disk space to cope with it.

When this process is complete, the next thing you have to do is to make sure your volume uses the new space allocated. You can do this by using the DISKPART command. You will find the necessary instructions here. That’s it!!! You will have your new drive with loads of space!!!