Thursday, April 29, 2010

LINQ

or should I say Ruby on Rails.NET?

Today I started experimenting with LINQ in the .NET 3.5 framework, and my first impression was "this looks so much like Ruby on Rails". I'm not complaining about this, because it makes my life soo much easier now that I can apply the knowledge I have about the Ruby on Rails framework with it's hashmaps and active directory to the .NET model with LINQ.

To demonstrate with a small piece of code that I have:
I have a List that is filled with Cell objects. Because I wish to make sure that each cell can only be added once to the List, I need to verify the names of the Cells in the list prior to adding the cell. So I made a special indexed property that uses a bit of LINQ.

The function body of my property looks like this:
public Cell this[String name]
So, if I wish to select the cell that matches the given name from my internal List object I need to call the following line of code:
return m_cells.Single(c => c.Name == name);
The line above will return a Single Cell object that has the specifed name and resides in the List collection. If no Cell matches the given LINQ expression and InvalidOperationException is thrown.

But because a property works in 2 ways, I need to make sure I can assign a cell as well. The logic behind the following function might be a bit weird, but I wish to be able to select a cell based upon it's name and assign it a new value. However I do not wish to add the same cell twice to the collection. To facilitate this I wrote the following code:

// Use LINQ to count the cells in the CellCollection with the specified name.
// We will perform a count on it, because we need to check if the cell is present.
Int32 cell_count = (from c in m_cells where c.Name == name select c).Count();
// Check if the specified cell was found in the collection.
if (cell_count == 0)
{
// Count 0 means the cell was not present in the first place, so
// add it at the end.
m_cells.Add(value);
}
else
{
// The cell is present, so locate it that we can overwrite it.
Cell cell = (from c in m_cells where c.Name == name select c).Single();

// Change the cell with the new value.
cell = value;
}
The above code does exactly what it's supposed to do. Overwrite the cell if it's already in the collection or add it at the end if it's not present.
LINQ makes writing code a bit trickier to understand, but it shortens the total amount of lines you need to write by ALOT.

Wednesday, April 14, 2010

Visual Studio

Been a while since I wrote on my blog, need to do this on a more regular basis...

So, at work we've finally started the move towards Visual Studio. It will be a long process to do, since all our software is written in the Borland IDE. I'm normally not a fighter for a specific compiler environment, but I can say that I do not like the Borland environment at all.
Perhaps it's Delphi, perhaps it's Borland itself. But I can never get the IDE to do what I want it to do, and the debugger in Borland is lacking when comparing it to the Visual Studio one.

The biggest challenge now will be to create the same functionality in Visua Studio that we had with the Delphi components from Borland. Although we will not achieve the same "lazy-mode", I'm pretty sure we can fine-tune our own DLLs and use then inside the .NET framework to make new cutting-edge applications.

This new Visual Studio 2010 looks promising. The new look&feel makes it more enjoyable to work in it then the previous versions. Only downside for me at the moment is the lack of intellisense in the C++ environment. During the Beta2 it was not present, and this is severly lacking for some operations. I really hope Microsoft puts it back in at some point.

I've also made some changes to my Blog. No more Anonymous comments from now i'm afraid. Too much moderation work of spam and unwanted ads on the blog. So if you wish to comment, you'll have to make an account I'm afraid.