Tuesday, May 6, 2008

System.Data.DataSetExtensions Build Error

Today I opened a Web Application (developed by someone else) from a client and tried to build it in Visual Studio 2008 and I got this build error:

Could not load file or assembly 'System.Data.DataSetExtensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.

Upon further investigation I figured out that the Framework 3.5 has changed the System.Data.DataSetExtensions from version 2.0.0.0 to 3.5.0.0. To resolve the problem,

1. Right-Click on the Web project and select "Property pages", The following window will load up and we can clearly see in the following screenshot that the System.Data.DataSetExtensions assembly is of the version 2.0.0.0.

2. Highlight the v2.0.0.0.0 assembly and re-add the same assembly using the Add button. The Property pages should now look as follows:
3. That's it you are done, atleast with this error....

In this blog article I would like a take a simple code example in C# and explore the different ways of expressing it using LINQ, Anonymous methods and Lambda Expressions.

Code Sample:
Let's start by defining a simple class Person

public class Person
{
public Person(string name, int age)
{
if(!string.IsNullOrEmpty(name) & age > 0)
{
Name = name;
Age = age;
}
}

public string Name { get; set; }
public int Age { get; set; }
}

As first step let's create a List<Person> and initialize with some people as follows:

static void Main(string[] args)
{
PopulateSample();
}

private static void PopulateSample()
{
persons = new List<Person>();

persons.Add(new Person("Joe", 40));
persons.Add(new Person("Steve", 35));
persons.Add(new Person("Mike", 29));
persons.Add(new Person("Dan", 45));
persons.Add(new Person("Jeff", 30));
}

Now that we have some data to work with as a next step let's try to find all the people in the list whose age is above 31 and print their names. As soon as try to write a solution to this, the first thing that comes to my mind is iterating through the list using a foreach loop. A very basic code sample would be as follows:


private static void ExecuteSampleUsingForEach()
{
Console.WriteLine("The Result using Foreach method:");
foreach(Person p in persons)
{
if(p.Age > 31)
{
Console.WriteLine(p.Name);
}
}

}

There is absolutely nothing wrong with this approach. But there are different methods of representing the same thing in code. The following methods may look very different but they all yield the same result.

private static void ExecuteSampleUsingAnonymousMethod()
{
IEnumerable<Person> peopleWithLongNames = persons.FindAll(delegate(Person p){return p.Age > 31; });
Console.WriteLine("The Result using Anonymous Method:");
foreach(Person person in peopleWithLongNames)
{
Console.WriteLine(person.Name);
}
}

private static void ExecuteSampleUsingLINQ()
{
IEnumerable<Person> peopleWithLongNames = from p in persons where p.Age > 31 select p;
Console.WriteLine("The Result using LINQ:");
foreach(Person person in peopleWithLongNames)
{
Console.WriteLine(person.Name);
}
}

private static void ExecuteSampleUsingLambdaExpressions()
{
IEnumerable<Person> peopleWithLongNames = persons.Where(p => p.Age > 31);
Console.WriteLine("The Result using Lambda Expressions:");
foreach(Person person in peopleWithLongNames)
{
Console.WriteLine(person.Name);
}
}

As a developer one of my goals is to write less but elegant code. With Framework 3.5 C# gives so many options to do the same thing. As developers it's up to us to use the right way at the right place.

Wednesday, April 23, 2008

Tortoise SVN - Ignoring unwanted file check-in

One of the annoyances with Tortoise SVN is keeping away from checking in binaries (*.exe, *.dll etc) and other unwanted files. There is neat trick that can help with this.

> Right-Click on any source controlled file/folder and select Settings. The following window should pop-up:

In the General > Subversion > Global ignore pattern filed enter all the extensions that you want to be ignored. A sample entry would be as follows:
*/bin */obj *.bak *.*scc *.user *.suo *.webinfo bin obj *.dll *.pdb *.exe

Sunday, April 13, 2008

Pittsburgh CodeCamp

A major portion of my day on Saturday was spent at Pittsburgh Code Camp. Being a not-so-eager-to-make-friends-at-new-places kinda guy I am I have never been to one of these code camps. It actually was pretty good. I got to meet some old friends.

Lessons learnt:

  • Don't be fooled by the presentation topic names, some of the topics are NOT as advanced as the topics suggest
  • Take my laptop next time I attend this so that when I get bored just write a blog or read some feeds using Google Gears (as there was no internet connection there)
  • It's not just about the presentations that are made, it's about meeting new people, catching up with old ones
  • If I am looking for a job this is where I wanna be
Some of the Presentations attended:
  • Getting Started with Silverlight 2 - John Juback from ComponentOne (presentation notes will be made available here in couple of days)
  • Refactoring in C# - Bad code to better code - Jonathan Cogley
  • Advanced Unit Testing with Mock Objects - Jeremy Jarrell (presentation notes is available here)
Overall it was a rewarding experience and I should really applaud and Pittsburgh .NET User Group & Pittsburgh Technical Council for conducting this code camp.

InputBox in C# and VB.NET

For developers with VB background InputBox may not be a new thing but for C# and VB.NET programmers developing Windows Forms (OR WinForms) this may seem alien. The reason this is not familiar to the WinForms developers is that it is not under the namespace "System.Windows.Forms" where all the other WinForms controls are available. InputBox is actually a method that can be used to collect some information from user. A screenshot of a input box is shown below:

In order to use InputBox, we need to add a reference to Microsoft.VisualBasic.dll

The method signature for InputBox looks as follows:
public static string InputBox( string Prompt, string Title, string DefaultResponse, int XPos, int YPos )

More information on the input parameters can be found here.

A sample usage in C# can be as follows:
string userInput = Microsoft.VisualBasic.Interaction.InputBox("Enter your name", "My Title", "default", 0, 0);

Note: If the user clicks cancel string.Empty is returned.

Thursday, April 10, 2008

Visual Studio 2008 Shell

Microsoft has made available the Visual Studio 2008 Shell for FREE, yup you read it right FREE. According to Microsoft:

"The Visual Studio Shell will be freely available as part of the Visual Studio SDK starting with the release of Visual Studio 2008. Building and deploying applications based on the Visual Studio Shell will be royalty-free."

You can build a DSL (Domain Specific Language) for any business analysis, integrate it with the shell, package it nicely and we have a Custom Business Modeling Tool. You can have your own product with the features of Visual Studio without writing any code.

The Visual Studio Shell is currently available in two modes:

  1. Isolated
  2. Integrated

More about the differences between the two modes can be found here

You can grab your free redistributable binary package of Visual Studio 2008 (Isolated) Shell here.

Tuesday, April 1, 2008

This is how it all began...

I have been wanting to BLOG for a long time. The only thing that I could not commit to is the subject. I wanted to pick a subject that will enable me to blog regularly. After a series of trials, the only subject that I could convince myself of is Software Development.

Being a full-time Programmer, I spend most of my day thinking, reading and developing software. I would like to use this blog to share my ideas, interesting findings etc. Hopefully you will find something interesting here to read...