Ludwig’s Blog

Adventures in .NET

Archive for the ‘General’ Category

Visual Studio 2010 style

Posted by Ludwig Stuyck on May 3, 2010

The default colors of visual studio are, well, kind of boring. But now it’s possbible to change that with only a few mouse clicks! First go to http://studiostyles.info and download your favorite style. Then, in visual studio 2010, select Tools > Import and Export Settings:

Check the Import selected environment settings, and click Next.

You probably want to save the current settings, so accept the default and click Next.

Then click the Browse button and select the template you downloaded:

…and then the Next button.

Finally click the Finish button.

And if everything was ok:

Click Close, and behold the new style :)

Posted in General | Leave a Comment »

Sort projects alphabetically in Visual Studio

Posted by Ludwig Stuyck on March 10, 2010

It is a well-known bug in Visual Studio that projects in solution folders are not sorted alphabetically. In solutions with a lot of projects this is often a problem, because it’s kind of hard to find the project you need. So I needed a way to permanently sort projects in solution folders, and it’s actually very easy to do.

Let’s say you have the following visual studio solution:

The project AAA was added after the project ZZZZZZ, so it’s not alphabetically sorted. In order to sort it, right click the ZZZZZZ project and select Rename. Then select another project to end the edit. As you will see, the projects will be sorted:

And then, and this is important, be sure to collapse the solution folders, and then quit and restart Visual Studio. You will then see that the projects remain sorted!

Posted in General | 1 Comment »

Creating a database table with zip codes

Posted by Ludwig Stuyck on December 16, 2009

Today I needed a database table in Sql Server that contains all belgian zip codes and city names. Obviously I was not going to create this by hand, so I wanted to share how I did it because I think it’s a common requirement.

The first step was to find a list of zip codes and city names. On the site http://www.post.be/site/nl/residential/customerservice/search/postal_codes.html you can download this list in excel format. From this file I extracted the zipcode and city name and saved this as a csv-file, which then looked like:

1000;Brussel
1000;Bruxelles
1005;Ass. Réun. Com. Communau. Commune
1005;Brusselse Hoofdstedelijke Raad
1005;Conseil Region Bruxelles-Capitale
1005;Ver.Verg.Gemeensch.Gemeensch.Comm.
1006;Raad Vlaamse Gemeenschapscommissie
etc... 

The second step was to create the database table that will contain this information, so I created a table called City with the following columns:

ZipCode, int
Name, varchar(100)

The next step was the actual import from the csv file into the table. To do this, I used the bulk copy technique: just open a new query window and execute the following statement:

BULK
INSERT City
FROM 'd:\postcodes.csv'
WITH
(
  FIELDTERMINATOR = ';',
  ROWTERMINATOR = '\n'
)

And now the table City is filled with 2903 records:

City table

City table

As a last step I added another column as the primary key:

Id, int, primary key (identity)

And now each entry has a primary key:

City table

City table

I think this is probably the easiest way to create a table with zip codes and city names. Except maybe… if someone would give you the script to create and fill the City table :) Well here it is (it seems I can only upload doc files, sorry for that): script_generate_zip_codes_belgium

Posted in General | 1 Comment »

Visual Studio: the coder’s visual experience

Posted by Ludwig Stuyck on November 17, 2009

Until now I have always used the standard font and color settings of visual studio: white background and courier new font.

Visual Studio default settings

Visual Studio default settings

It was fine, but not spectacular. And recently I saw someone using a black background, and that was kind of interesting. So I decided to tweak my visual studio to try it out for myself.

First, the default font (courier new) was boring. If found an interesting true type font called ‘Anonymous’ on http://www.ms-studio.com/FontSales/anonymous.html: a well balanced and nice to read font especially created for coders. That’s us :) So I downloaded it, installed it and configured Visual studio to make use of this font.

 I also like line numbers to be displayed, to I turned it on. And finally, the colors. Black and white backgrounds are two extremes and I find them rather fatiguing and too much contrast, so I used a dark gray color. Also:

  • Plain text: white item foreground on a dark gray background
  • Comments: magenta
  • Identifier: yellow-ish
  • Keyword: orange
  • Operator: white
  • String: lime
  • Usertypes: olive

Visual Studio tweaked settings

Visual Studio tweaked settings

I may tweak it some more in the coming days, but we’ll see how it turns out. But I like the change: it’s more relaxing for the eyes but still clear to read.

Posted in General | 1 Comment »

Dummy text generator

Posted by Ludwig Stuyck on May 14, 2009

Designing your web site also involves making sure that your lay-out works well in various screen resolutions and sizes, and afterwards this design has to be validated by users. To do that, you need dummy text you can use to fill up the parts of a web page. Recently I stumbled upon http://www.blindtextgenerator.com, which generates this for you. If you use this, users who have to validate the design will most likely focus on the design instead on the content – because the content is meaningless. In other words: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Understood? :)

Posted in General | Leave a Comment »

I love lambda expressions (part 2)!

Posted by Ludwig Stuyck on April 14, 2009

In previous part we learned about lamda expressions, and ended up with the following example:

List<int> numbers = new List<int> { 1, 7, 16, 23, 41, 66 };
Func<int, bool> funcNumbersSmallerThan25 = n => n < 25;

The delegate funcNumbersSmallerThan25 is compiled into executable code and can be called as follows:

List<int> numbersSmallerThan25 =
      numbers.Where(funcNumbersSmallerThan25).ToList();            

Expression trees

Now it’s time to introduce another feature called expression trees. An expression tree is a tree structure that contains lambda expressions. To demonstrate, let’s define the delegate as an expression using the following syntax:

Expression<Func<int, bool>> exprNumbersSmallerThan25 = n => n < 25;

Now the delegate exprNumbersSmallerThan25 is no longer compiled to executable code, but instead compiled as data. In order to use it, you have to compile it to IL first:

List<int> numbers = new List<int> { 1, 7, 16, 23, 41, 66 };
Expression<Func<int, bool>> exprNumbersSmallerThan25 = n => n < 25;
var originalDelegate = exprNumbersSmallerThan25.Compile();
List<int> numbersSmallerThan25 = numbers.Where(originalDelegate).ToList();    

If you call the ToString() method of the expression:

string lambda = exprNumbersSmallerThan25.ToString();

…you get back the lamda expression of the delegate:

Lambda expression

Lambda expression

So what we did was create a delegate exprNumbersSmallerThan25  using the lambda expression ‘n => n < 25′, compiled it to IL code and used it. Instead of writing it using the lamba expression, you can also write it without the lamda expression:

List<int> numbers = new List<int> { 1, 7, 16, 23, 41, 66 };
ParameterExpression n = Expression.Parameter(typeof(int), "n");
Expression body = Expression.LessThan(n, Expression.Constant(25));
Expression<Func<int, bool>> exprNumbersSmallerThan25
                       = Expression.Lambda<Func<int, bool>>(body, n);
var originalDelegate = exprNumbersSmallerThan25.Compile();
List<int> numbersSmallerThan25 = numbers.Where(originalDelegate).ToList();

Actually this is exactly what internally is going on. Of course you would use lambda expressions because obviously it’s a more clear and shorter way to define an expression.

Expression tree viewer

To visualize expression trees, you can use the Expression Tree Visualizer tool that is part of the Visual Studio 2008 CSharp samples. To use it from Visual Studio, you have to build it first and copy the required DLL so that Visual Studio can use the visualizer:

  • First choose Help | Samples from the Visual Studio 2008 menu, click the ‘local Samples folder’ link and unzip the CSharpSamples/LinqSamples/ExpressionTreeVisualizer solution. 
  • Build the solution and then copy the bin\debug\ExpressionTreeVisualizer.dll assembly to the ‘Visual Studio 2008\Visualizers’ directory (to find out the directory on your system, select  Tools | Options | Projects and Solutions | General  from the Visual Studio menu)
  • Restart Visual Studio

You can now test the visualizer by debugging your test project, hovering your mouse above an expression variable and clicking the magnifying glass:

Expression magnifying glass

Expression magnifying glass

 Click the magnifying glass to view the expression tree:

Expression Tree Viewer

Expression Tree Viewer

Why use expression trees?

Expression trees provide an efficient data representation of lambda expressions (which are functions, ultimately) – so they are data structures that represent executable code. You can use them to interact with data at a low level: they can be evaluated, parsed and changed during runtime, and transformed to IL code. But why would we want to do such conversions?

To take an example, LINQ2SQL makes heavy use of expression trees. The following is a very simple example of a LINQ2SQL expression:

var employees = from e in db.Employees
                where e.City == "Brussels"
                select e;

What happens is that this expression is converted into an data structure (expression tree), and then this expression tree is analyzed by the LINQ compiler to decide the best way to convert it to SQL statements, which will be something like: 

SELECT [t0].[City], [t0].[EmployeeName]
FROM [db0].[Employees] AS [t0]
WHERE [t0].[City] = @p0

So it’s nothing more than a transformation from expresssion -> data structure -> string representation. The goal of these transformation steps is to generate a data structure that is abstract, so that another process can handle it: the LINQ compiler can analyze and parse it in any way it wants (optimized for data access), or you can pass the same expression tree to some other piece of code that transforms it to something complete different, optimized for something else.

Posted in General | 1 Comment »

Tips and tricks (2)

Posted by Ludwig Stuyck on April 9, 2009

The ?? operator is called the null-coalescing operator and is not very well-known, but can be very useful sometimes to define default values for types as in the following example:

int? number1 = null;
// number2 = number1, unless number1 is null, in which case number2 = -1
int number2 = number1 ?? -1;

If number1 is null then number2 will be set to -1, if number1 is not null, number2 will get the value of number1. A useful case where this can be applied is in properties, to guarantee that a collection is initialized:

private List<int> numbers = null;
public List<int> Numbers
{
  get { return numbers ?? new List<int>(); }
}

You can even do a number of comparisons in one line of code:

DateTime? dateTime1 = null;
DateTime? dateTime2 = null;
DateTime? dateTime3 = DateTime.Now;
DateTime dateTime = dateTime1 ?? dateTime2 
                    ?? dateTime3 ?? DateTime.MinValue;

This obviously avoids a large block of if-then-else statements.

Posted in General | Leave a Comment »

Tips and tricks (1)

Posted by Ludwig Stuyck on April 8, 2009

The following code is a straightforward example on how to use the event programming model: the main method is the subscriber that will receive ValidationCompleted notifications, and the Validation class is the publisher that publishes the ValidationCompleted event:

class Program
{
  static void Main(string[] args)
  {
    Validation validation = new Validation();
    validation.ValidationCompleted += 
        new Validation.ValidationHandler(validation_ValidationCompleted);
    validation.Execute();
  }
  static void validation_ValidationCompleted(object sender, 
                                                     bool succeeded)
  {
    Console.WriteLine(string.Format("Validation result: {0}", succeeded));
  }
}
public class Validation
{
  public delegate void ValidationHandler(object sender, bool succeeded);
  public event ValidationHandler ValidationCompleted;
  public void Execute()
  {        
    // do some validation here
    bool validationSucceeded = true;
    SignalValidationCompleted(validationSucceeded);
  }
  protected void SignalValidationCompleted(bool succeeded)
  {
    if (ValidationCompleted != null)
    {
      ValidationCompleted(this, true);
    }
  }
}

If you run this, the result is what you expect:

Validation output

Validation output

Notice in the Validation publisher class the test to check for null:

if (ValidationCompleted != null)
{
  ValidationCompleted(this, true);
}

You have to do this because when there are no subscribers, the ValidationCompleted event will be null.

A nice trick to avoid the need for this null-test, is to add an empty delegate to the event declaration:

public event ValidationHandler ValidationCompleted = delegate {};

This way, the event will never be null and you can just call it:

protected void SignalValidationCompleted(bool succeeded)
{
  ValidationCompleted(this, true); // No null check needed anymore
}

A nice little trick.

Posted in General | Leave a Comment »

I love lambda expressions (part 1)!

Posted by Ludwig Stuyck on April 1, 2009

In the beginning, you are nervous, not sure and even afraid. Slowly you get used to it by experimenting and finally you can’t live without it anymore. To avoid confusion, I’m talking about lamda expressions here.  To prove my point, let’s first go back into time.

If we would need to filter out a list of numbers to get all the numbers smaller than 25, following code does that fine:

List<int> numbers = new List<int> { 1, 7, 16, 23, 41, 66 };
List<int> numbersSmallerThan25 = new List<int>();
foreach (int number in numbers)
{ if (number < 25) { numbersSmallerThan25.Add(number); } }

Using predicate delegates

Of course, we can do a lot better, by creating a method that checks if a passed number is smaller than 25, and passing this method as a parameter to the FindAll method of the list:

static void Main(string[] args)
{
   List<int> numbers = new List<int> { 1, 7, 16, 23, 41, 66 };
   List<int> numbersSmallerThan25 =
                    numbers.FindAll(MatchNumberSmallerThan25);
}
private static bool MatchNumberSmallerThan25(int number)
{
  return number < 25;
}

Here we make use of predicate delegates. A predicate delegate is a method that returns true or false. In this case we define a MatchNumberSmallerThan25 method, which returns true if the passed number is smaller than 25. Now we can pass this predicate to the FindAll method of the generic list. As a result, the FindAll method executes the MatchNumberSmallerThan25  method for each item in the generic list. For each call that returns true, the collection item is added to the returning collection of the FindAll method.

Using anonymous methods

This already seems a lot better, however, we still have to create private methods for every case we have, methods that we probably never reuse. So we could improve this by using anonymous methods:

 List<int> numbers = new List<int> { 1, 7, 16, 23, 41, 66 };
 List<int> numbersSmallerThan25 = numbers.FindAll(
                delegate(int number) { return number < 25; } );

In this example we are passing an anonymous inline piece of code as a predicate delegate to the FindAll method, which does exactly the same as the MatchNumberSmallerThan25 method. This offers us a number of advantages: no need to clutter your code with private named methods, the code is where it is used, no need to specify the return type (it is inferred from the signature of the delegate type to which the anonymous method is being cast to) and you can refer to the outer variables from within the anonymous method.

Using lambda expressions

As the title of this blog suggests, we still have a way to improve the code above. So let’s dot that:

static void Main(string[] args)
{
  List<int> numbers = new List<int> { 1, 7, 16, 23, 41, 66 };
  List<int> numbersSmallerThan25 = numbers.FindAll(n => n < 25);               
}  

Did you notice the amount of code we have left? And this is all thanks to the beloved lambda expressions! At first it seems a bit cryptic, so what is a lambda expression? It’s nothing more than an anonymous method used to create a delegate, and that takes takes the form:

argument-list => expression

In our example, the lambda expression consists of an argument named ‘n’ (which is implicitely typed as an integer), then the lambda operator ‘=>’, followed by an expression ‘n < 25′.

You could read it as: ‘There’s a method that takes one parameter n and a list of integers; and it returns every number from that list that is smaller than 25 ’. This lambda expression is then passed to the FindAll method of the generic collection. Actually, this lambda expression is a very small, compact, anonymous method that is another way of writing an anonymous method, which is also more readable and verbose.

A closer look

If we look at the FindAll signature, we see that it expects a parameter of type Predicate<T>, and because we are applying it to a list of integers, it has to be of type Predicate<int>. So we could explicitely create and use such predicate as follows:

List<int> numbers = new List<int> { 1, 7, 16, 23, 41, 66 };
Predicate<int> predNumbersSmallerThan25 = n => n < 25;
List<int> numbersSmallerThan25 = numbers.FindAll(predNumbersSmallerThan25);  

However, FindAll is a .NET 2.0 method, and the preferred way is using the .NET 3.5 Where method, which expects a Func<T1, TResult> parameter. T1 is the type of the parameter (which is int) and TResult is the type of the returned value (which is bool). So we end up with the following code, which has exact the same results as the previous code:

List<int> numbers = new List<int> { 1, 7, 16, 23, 41, 66 };
Func<int, bool> funcNumbersSmallerThan25 = n => n < 25;
List<int> numbersSmallerThan25 = 
               numbers.Where(funcNumbersSmallerThan25).ToList();

Other examples

Now that you have fallen in love with lambda expressions too, some other examples:

// Add 5 to every number in the collection
List<int> numbersAdded5 = numbers.ConvertAll(n => n + 5);
// Sort list descending (lambda expression with two arguments!)
numbers.Sort( (a,b) => b-a );
// Groups collection of numbers into two groups containing
// odd or even numbers
var grouped = numbers.GroupBy(n => n%2==0);

Note the ‘var’ keyword in the last example. It means that the compiler will infer the type for you, it does not mean that it’s some kind of variant data type that can change. In this case, the grouped variable will always be of type ‘System.Linq.GroupedEnumerable<int,bool,int>’, because that is the return type of the GroupBy method.

There’s more to learn about lambda expressions… in the second part!

Posted in General | 2 Comments »

Modifying namespace in XML document programmatically

Posted by Ludwig Stuyck on March 31, 2009

I needed to validate an XML document with a given XSD document. Seems easy enough… so let’s have a look at the schema first: 

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"           
                 xmlns="http://my.namespace"          
                 elementFormDefault="qualified"           
                 targetNamespace="http://my.namespace"> 
  <xs:element name="customer">   
    <xs:complexType>     
      <xs:sequence>       
      <xs:element name="firstname" type="xs:string" />       
      <xs:element name="lastname" type="xs:string" />       
      <xs:element name="age" type="xs:integer" />     
      </xs:sequence>   
    </xs:complexType> 
  </xs:element>
</xs:schema>

The XML instance is:

<?xml version="1.0" encoding="utf-8" ?>
<customer>
  <firstname>Homer</firstname>
  <lastname></lastname>
  <age>36</age>
</customer> 

 The code is straightforward: 

static void Main(string[] args)
{
  // Load the xml document
  XDocument source = XDocument.Load(@"instance.xml");
  // Load the schema
  XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();
  xmlSchemaSet.Add(null, XmlReader.Create(@"customer.xsd"));
  // Validate
  try { source.Validate(xmlSchemaSet, ValidationCallback, true); }
  catch (Exception ex) { Console.WriteLine(ex.Message); }
}
static void ValidationCallback(object sender, System.Xml.Schema.ValidationEventArgs e)
{
  Console.WriteLine(string.Format("[{0}] {1}", e.Severity, e.Message));
} 

If you run this, no errors are thrown so it seems to validate. To be sure, let’s change the age in an invalid value:

<Age>invalid!</Age>

and test again. Well… actually, no validation error is thrown in this case either… what’s going on here?

Actually, the XML is not validated at all, because it’s not in the same namespace (http://my.namespace) as the schema definition. This is very dangerous, as we might easily get mislead by thinking that it validates because no errors are thrown. So how do we solve it?

We could ask the sender to provide the correct namespace in the XML file – this would be the best solution because then it would just work – if you try to validate the following XML:

<?xml version="1.0" encoding="utf-8" ?>
<customer xmlns="http://my.namespace">
  <firstname>Homer</firstname>
  <lastname></lastname>
  <age>invalid</age>
</customer>

…then the validation error is thrown, because the namespaces now match:

Validation error

Validation error

Unfortunately, it is not always possible to change the XML file, so how can we bypass this namespace conflict? If appears that if we would change the namespace in the loaded XML document to the one we are using in our schema, the conflict is resolved. A first attempt may be:

// Load the xml document
XDocument source = XDocument.Load(@"instance.xml");
// Change namespace to reflect schema namespace
source.Root.SetAttributeValue("xmlns", "http://my.namespace");
// Load the schema
XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();
xmlSchemaSet.Add(null, XmlReader.Create(@"customer.xsd"));
// Validate
try { source.Validate(xmlSchemaSet, ValidationCallback, true); }
catch (Exception ex) { Console.WriteLine(ex.Message); } 

If we run this, the validation error is still not thrown, so setting the namespace attribute is not enough. The reason is that once the XDocument is loaded, every element in the tree gets prefixed with the namespace name. So we need to change them all, and so I wrote the following method that does this:

static void Main(string[] args)
{
  // Load the xml document
  XDocument source = XDocument.Load(@"instance.xml");
  // Change namespace to reflect schema namespace
  source = SetNamespace(source,"http://my.namespace");
  // Load the schema
  XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();
  xmlSchemaSet.Add(null, XmlReader.Create(@"customer.xsd"));
  // Validate
  try { source.Validate(xmlSchemaSet, ValidationCallback, true); }
  catch (Exception ex) { Console.WriteLine(ex.Message); }
}
public static XDocument SetNamespace(XDocument source, XNamespace xNamespace)
{
  foreach (XElement xElement in source.Descendants())
  {
    // First make sure that the xmlns-attribute is changed
    xElement.SetAttributeValue("xmlns", xNamespace.NamespaceName);
    // Then also prefix the name of the element with the namespace
    xElement.Name = xNamespace + xElement.Name.LocalName;
  }
  return source;
}
static void ValidationCallback(object sender, System.Xml.Schema.ValidationEventArgs e)
{
  Console.WriteLine(string.Format("[{0}] {1}", e.Severity, e.Message));
} 

The SetNameSpace method will set the corrrect namespace for each element in the XDocument. And if we run it now, the validation error is thrown again because the namespace in the XDocument has been modified and matches the schema namespace.

Posted in General | 1 Comment »

 
Follow

Get every new post delivered to your Inbox.