Monday, May 12, 2008

Extending IDataReader implementations using extension methods

In my post How to extend classes using extension methods, I write about how to extend classes with your own methods. This time we will extend the IDataReader interface with some new methods to save some typing. Since we extend the interface, our methods will be available in all the classes implementing the IDataReader interface, such as the SqlDataReader.

The IDataReader interface provides methods for getting field values for a given type:


string email = reader.GetString(0);

The problem with these methods are that they only accept the index-position and not the field name. Instead, I would prefer to write the code above like this:


string email = reader.GetString("Email");

In order to do this, we need to create a new static class, which defines some new extension methods:


using System;
using System.Data;

/// 
/// Provides extension methods for the IDataReader interface.
/// 
public static class IDataReaderExtension
{
 public static bool GetBoolean(this IDataReader reader, string name)
 {
  return (bool) reader[name];
 }
 public static Guid GetGuid(this IDataReader reader, string name)
 {
  return (Guid) reader[name];
 }
 public static int GetInt(this IDataReader reader, string name)
 {
  return (int) reader[name];
 }
 public static double GetDouble(this IDataReader reader, string name)
 {
  return (double) reader[name];
 }
 public static string GetString(this IDataReader reader, string name)
 {
  return (string) reader[name];
 }
 public static bool IsDBNull(this IDataReader reader, string name)
 {
  if (reader[name] == DBNull.Value)
   return true;

  return false;
 }
}

Since we didn't put our static class within a namespace, then our extension methods will be available at once. However, if you want to use a namespace, then you need to import the namespace in every class where you want to use these extension methods.

Sunday, May 11, 2008

How to add reflection to texts, shapes and pictures in Powerpoint 2007

Text reflection:
1. Start by adding some text to your Powerpoint slide.
2. Select the text you want to add reflection to.
3. Click on the tab Format.
4. Select Text Effects, then Reflection and choose the reflection variant you prefer.

Shape reflection:
Same as text reflection, but choose Shape Effects instead.

Picture reflection:
Same as text reflection, but choose Picture Effects instead.

Sunday, May 4, 2008

How to extend classes using extension methods

In .NET 3.5 there is a new functionality called extension methods. Extension methods let you extend any classes in the .NET framework, 3rd party frameworks or your own frameworks with your own methods. Let's look at an example where we extend the System.String class with two new methods, to get the first five and last five characters of a string:

public static class StringExtension
{
 /// 
 /// Returns the first five characters of a string.
 /// 
 public static String GetFirstFiveCharacters(this String myString)
 {
  return myString.Substring(0, 5);
 }
 /// 
 /// Returns the last five characters of a string.
 /// 
 public static String GetLastFiveCharacters(this String myString)
 {
  return myString.Substring(myString.Length - 5, 5);
 }
}

In the example above, we use a class with the name StringExtension. However, you can name this class whatever you like: StringExtension, StringMethods, StringHelper, Helper etc. The only important thing is that your methods follow the pattern for extension methods:


public static  MethodName(this  variableName)

Now, let's produce some code using our extension methods:


String helloWorld = "Hello World";
String hello = helloWorld.GetFirstFiveCharacters(); // contains Hello
String world = helloWorld.GetLastFiveCharacters(); // contains World

Of course, extension methods are shown in the intellisense in Visual Studio.

Thursday, May 1, 2008

The C# using statement

In C#, when you want to make sure an object is disposed, then you might place you're code within a try finally block as follows:
SqlCommand command = new SqlCommand();
try
{
 command.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
 command.CommandText = "delete from dbo.Customer;";
 command.Connection.Open();
 command.ExecuteNonQuery();
}
finally
{
 command.Dispose();
}
However, a nicer and shorter way of writing the exact same code is by using the using statement as follows:
using (SqlCommand command = new SqlCommand())
{
 command.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
 command.CommandText = "delete from dbo.Customer;";
 command.Connection.Open();
 command.ExecuteNonQuery();
}
The using block will only work with objects of classes implementing the IDispose interface. In the case of the SqlCommand used in the code above, the Dispose method closes the database connection if it's open and clears any resources used.