Professional Programmer Notes

or just call this my soapbox

Archive for May 2009

Rails-like ASP.NET Development Assets

with 2 comments

Here are the slides:

Here is the ASP.NET code

*Note: Sqlite ADO.NET Provider is required to run this code. You can get it from http://sqlite.phxsoftware.com/

Written by curtismitchell

May 31, 2009 at 11:12 am

Hello world!

with one comment

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Written by curtismitchell

May 16, 2009 at 5:37 pm

Posted in Uncategorized

Debug an IE Add-On in Visual Studio 2008

leave a comment »

I’m working on a project that involves creating an IE Add-On in C#.  It is an inherited project that was created in Visual Studio 2005 targeting the .NET Framework 2.0.

Well, times have changed.  I am now doing most of my development in Visual Studio 2008, and (when lucky) I am using the features of the .NET Framework 3.0 or higher.  When I first made the switch, I tried to use 2008 to continue my work on the toolbar, but I failed miserably.  I was shocked to find out that my process of registering my assembly in a post-build action and launching IE as a Start Action did not work in 2008.  At the time, I quickly got over it, and preserved 2005 just for that project.

Luckily, in a more recent attempt to use 2008 for this type of development, I was able to solve the problem.  Check it out.

Problem:

Whenever I tried to debug my IE Toolbar project in Visual Studio 2008, I would get a nasty exception that says, “Error while trying to run project.  Unable to start debugging

image

My project is configured to register the assembly using regasm in a Post-build step.  Then, as you can see below, the VS Debugger is configured to start Internet Explorer (and subsequently attach to the process).

image1

Well, that was the wrong way of doing it.  I was able to get it to work successfully by modifying the Start Action like such:

image

And it worked!

UPDATE:  I still had issues on a separate computer.  Adding “about:blank” to the command line arguments fixed the issue.

Written by curtismitchell

May 6, 2009 at 3:25 pm

Ruby-like Times method for Ints in C#

leave a comment »

Yesterday, @mccartsc did a presentation on Linq for a group of us at work. As part of his presentation, he demonstrated an extension method he threw together to give .NET Integers the Times method that Ruby programmers have enjoyed for years.

Basically, the n.Times methods is passed a block that it will execute n number of times. In Ruby, you could do something like this:

5.times {|x| puts x }

That trivial line of code would output integers 0 through 4.

With such a trivial use case, you may be wondering “Why would anyone want to do that?” Well, @mccartsc and I had a discussion about coding without traditional For Loops. Foreach Loops are great for enumerating IEnumerable objects, but it is not a replacement for the traditional For Loop. We thought, “Wouldn’t it be great if you could use a Ruby-like Time method to execute a block of code an arbitrary number of times?” So, @mccartsc built it as part of his Linq demonstration.

Here’s how he did it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqDemo
{
    static class Extensions
    {
        public static void Each<T>(this IEnumerable<T> collection, Action<T> action)
        {
            foreach (var item in collection)
                action(item);
        }

        public static IEnumerable<int> Range(this int max)
        {
            for (int i = 0; i < max; i++)
                yield return i;
        }

        public static void Times(this int i, Action<int> action)
        {
            i.Range().Each(action);
        }
    }
}

@mccartsc created three extension methods to implement the Times method. “Each” is an extension method for IEnumerable types. It passes each member of a collection into an Action delegate.

“Range” is an extension method for Int types. It creates a zero-based IEnumerable collection out of an integer. For instance, 5.Range();, would return a collection consisting of integers 0,1,2,3,4.

Finally, “Times” is another extension method for Int types that allows a user to execute an action an arbitrary number of times by using the aforementioned Range and Each methods. Once I have these extension methods in my C# project, I can execute code like this:

5.Times(i => Console.WriteLine(i.ToString()));

The above would output:
0
1
2
3
4

I think that’s pretty cool.
Thanks @mccartsc for the code and the demonstration.

Update: @mccartsc got a blog! Check him out at http://scmccart.wordpress.com/.

Written by curtismitchell

May 1, 2009 at 9:13 pm

Posted in Uncategorized

Tagged with , ,