this.Blog.Find(entry => entry.IsHelpful);
 Wednesday, May 14, 2008
C# Extension Methods

In my last post, I showed an example of how you can create an extension method on an existing class in javascript.  Well, C# 3.0 has now added the exact same functionality.  And it just so happens that today I needed to create one, so I thought I would post an example of how it's done in C#.

public static class StringExtensions 
{
public static byte[] ToByteArray(this string s)
{
return ASCIIEncoding.UTF8.GetBytes(s);
}
}

Extension methods act like static methods on existing classes, and as such they can only be declared in static classes. To mark a method as an extension method, you must preface the declaration of the first parameter in the method signature with the keyword "this". The compiler will then add the method to the type of class declared in the first parameter. Any other parameters declared after the first one will become the signature to the extension method.

Now we can call the extension method we added to the String class:

public class Program 
{
public static void Main(string[] args)
{
string myString = "Foo";
byte[] bytes = myString.ToByteArray();
}
}

The other cool thing is that Visual Studio provides full Intellisense on all extension methods!  So in the example I provided, ToByteArray() is now offered up as a potential method anytime I reference a string.


Kick it on DotNetKicks.com
Wednesday, May 14, 2008 12:43:26 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  .Net | C#

 Tuesday, May 13, 2008
Implementing String.trim() in Javascript

Curiously, the String class in javascript does not natively expose a trim() method.  Not a problem!  Since it is a dynamic language, javascript allows you the ability to crack open a class and add new functionality to it.

<script language="javascript" type="text/javascript">

String.prototype.trim = function() {
  return this.replace(/^\s+/,"").replace(/\s+$/,"");
}

var s = new String(" Test me! ").trim();

alert("'" + s + "'");

</script>

The prototype keyword indicates that you are adding functionality to an existing class. In this case, we are adding a method called trim() to the String class that uses regular expressions to find any spaces  (\s+ = one or more whitespace characters) at the beginning (^ = match at the START of a line) and then the end ($ = match at the END of a line) of the string, and replace the spaces we find with an empty string ("").


Kick it on DotNetKicks.com
Tuesday, May 13, 2008 11:07:44 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  Javascript

 Monday, May 12, 2008
Visual Studio 2008 and .Net 3.5 SP1 Beta Released

Microsoft announced today the release of the first public beta of Service Pack 1 for Visual Studio 2008 and the 3.5 version of the .Net framework.  Rather than an extended re-hash of what's included in the release, I'll defer to the experts:

Some of the highlights for me are:

  • Rails-like "Scaffolding" support via ASP.Net Dynamic Data (I need to play around with this and see if there's a need to get Miado to work with this)
  • ASP.Net AJAX script combining to reduce the number of requests to the server
  • ASP.Net AJAX back button support!
  • Improved Javascript intellisense and formatting
  • Support for ADO.Net Data Services (the project formerly known as Astoria), which aids in the creation of a data access layer using REST-ful web services

Since this is a *beta* version of a service pack, at this point I personally wouldn't install this in anything but a VM.


Kick it on DotNetKicks.com
Monday, May 12, 2008 4:45:45 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  .Net | Visual Studio

 Friday, May 09, 2008
Best line on TV last night

From The Office (of course):

"Andy Bernard, Cornell '95"

I need to look for him at the next reunion...


Kick it on DotNetKicks.com
Thursday, May 08, 2008 11:21:04 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  Cornell | Humor | TV

 Thursday, May 08, 2008
JavaFX announced (yet again) at this year's JavaOne

In a stunning move at this year's JavaOne conference, Sun announced the upcoming release of JavaFX, its framework for building Rich Internet Applications (RIAs).  Stunning, you see, because they announced the same thing at *last year's* JavaOne conference.

I read the headline and thought to myself, "Hmm ... this must be some mistake."  But, no, it was not.  Apparently, 2007 was *not* the year of Java on the desktop, and we are forced to endure Sun's desperation pleas of, "All is well!!!" for one more year.  Now, they enter an increasingly crowded field of RIA frameworks, with Flash firmly entrenched and Silverlight gaining momentum.

Java has never had a strong success story when it comes to building GUI applications, despite the ironic fact that the initial vision of Java was that its strength would be on the client-side due to its "write once, run anywhere" cross-platform JVM.  The thought being you could write one client application and distribute that application across Windows, Mac, Linux, etc...  Well, that vision has never materialized.  Swing applications were cumbersome, buggy, resource intensive, slow (with horrendous start-up times), and let's face it, just down right ugly.  Users wanted native-looking apps, and Swing was like a herd of pink flamingos poised conspicuously in the desktop front yard.

Swing has come a long way since its initial inception, but unfortunately for Sun, they haven't been able to shake users' initial perceptions.  In another ironic fact, perhaps the weaknesses of building rich UIs in Java led to its unbelievable success in the late 1990s/early 2000s as a server-side language and for developing web-based applications.  The browser turned out to be a better cross-platform GUI than Swing, plus it required no installation or deployment concerns.

Now, here we are in 2008, and Java is looking a little long in the tooth, even for building web-based applications.  Steve Jobs famously declared Java as being dead (i.e. your act is about as fresh as a Foghat concert) as his reasoning for not including it in the iPhone.  I wouldn't go that far.  Java is so entrenched (especially in Fortune 500 companies) that I'd venture to say that it's going to outlive me.  Ha!  Take that, Steve Jobs!  But as far as building next-generation UIs, as a developer I am left scratching my head as to why the world needs a Java-based RIA so late in the game when Java has never had success in that space in the first place.

Let's hope in 2009, the JavaOne crowd is not left asking, "Thank you, sir, may I have another?" as Sun announces, "JavaFX - this time we *really* mean it!".


Kick it on DotNetKicks.com
Thursday, May 08, 2008 9:36:44 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  Conferences | Java | Silverlight

 Monday, May 05, 2008
Reason #134 as to why you need to be reading Wil Wheaton's blog

Wil Wheaton is fantastic.  This quote is from one of his recent posts questioning his own thoughts on JJ Abrams' promise to "re-invent" the Star Trek series with the new movie:

"Speaking as a lifelong geek, my knee-jerk reaction when I hear someone talking about 'reinventing' something like Trek is that it will be a tower of suck, built out of an endless supply of Jar-Jars and midichlorians."


Kick it on DotNetKicks.com
Monday, May 05, 2008 9:45:29 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  Humor