KBD

Keith Devens .com

Wednesday, December 3, 2008 Flag waving
Men never do evil so completely and cheerfully as when they do it from religious conviction. – Blaise Pascal (attributed)

Archive: August 20, 2008

← August 19, 2008August 21, 2008 →

Daily link icon Wednesday, August 20, 2008

More fun with extension methods

or shoehorning in what the C# library should have included in the first place.

public static void Each<T>(this IEnumerable<T> source, Action<T> a){
  foreach (var item in source)
    a(item);
}

public static void Each(this IEnumerable source, Action<object> a){
  foreach (var item in source)
    a(item);
}

See my previous extension method, Join.

Bad C# design

I should be able to do something like:

var table = new HtmlTable {
  Rows = { // <-- error is here
    from XmlNode p in node.SelectNodes("./property")
    select new HtmlTableRow{
      Cells = {
        new HtmlTableCell{ InnerText = Server.HtmlEncode(p.Attributes["description"].Value) },
        new HtmlTableCell{ InnerText = Server.HtmlEncode(p.InnerText) }
      }
    }
  }
};

But I can't, because the LINQ code isn't "expanded" so the compiler complains that while it expects an HtmlTableRow element between the braces assigned to Rows, you're giving it a collection. But, you can't assign the collection to Rows directly because it's a read-only property.

Bad class design (Rows being read-only... same as Cells fwiw) or bad language design in that there's no way to "expand" the results of the LINQ query to do what seems natural in that spot?

Instead, I have to do:

var rows = 
  from XmlNode p in node.SelectNodes("./property")
  select new HtmlTableRow{
    Cells = {
      new HtmlTableCell{ InnerText = Server.HtmlEncode(p.Attributes["description"].Value) },
      new HtmlTableCell{ InnerText = Server.HtmlEncode(p.InnerText) }
    }
  };

var table = new HtmlTable();
foreach (var r in rows)
  table.Rows.Add(r);

which is redundant.

  1. Lifehacker: Manage Tasks and Calendars from Gmail using Remember the Milk and Firefox plugins.

       (0) Tags: [Firefox]
← August 19, 2008August 21, 2008 →
December 2008
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 4 posts

Recent comments XML

Perl 6 1.0 in March?

Doh, my mistake. I'm aware of the​relation between Parrot and Rakudo​but I'...

Keith: Dec 2, 1:03am

Free image hosting sites

Well, TinyPic has this in its​FAQ:

> Images and videos is in​your accoun...

Keith: Dec 1, 1:13am

Join a NameValueCollection into a querystring in C#

Well with a lamba expression, this​is what I came up​with:

?!code:csharp...

Gustaf Lindqvist: Nov 30, 4:38pm

Why no generic OrderedDictionary?

Check​http://www.codeproject.com/KB/recip​es/GenericOrderedDictionary.aspx?d...

Gabrielk: Nov 27, 6:57am

Generated in about 0.047s.

(Used 7 db queries)

mobile phone