 Wednesday, May 21, 2008
Using LINQ to generate a random confirmation number
Igor Ostrovsky recently published a great blog post about 7 tricks for using LINQ to simplify your programs. Posts like this really make me re-evaluate how I am writing my code in C# 3.0. For example, I had written a piece of code for my current project that generates a random confirmation number for a reservation system. After reading Igor's post, I built off one of his examples and refactored my code to use LINQ to build my confirmation number. I must admit, the refactored code is much more elegant:
private static readonly char[] AVAILABLE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890123456789".ToCharArray();
private static string GenerateConfirmationNumber(int length) { int lastIndex = AVAILABLE_CHARS.length - 1; Random rand = new Random(); var confNbr = Enumerable.Repeat<int>(0, length) // loop N times .Select<int, int>(loopNbr => rand.Next(0, lastIndex)) // get a random index .Select<int, char>(index => AVAILABLE_CHARS[index]) // pull a char .ToArray(); // project the results to a char[] return new String(confNbr); }
I think it's important to remember that LINQ is much, much more than just LINQ-to-SQL (which is what most people immediately think of when you refer to LINQ). In fact, none of the examples in Igor's post have anything to do with SQL. Take a look at his post (and other LINQ examples online) and hopefully it will give you ideas on how to code using more of a dynamic language type syntax.
Wednesday, May 21, 2008 9:42:57 AM (Eastern Standard Time, UTC-05:00) .Net | C# | Linq
 Saturday, May 17, 2008
String extension methods in ASP.Net Ajax
In an earlier post, I showed you how you could add a trim() method to the String class in Javascript. Well, as I found out late last week, if you are using the ASP.Net Ajax framework, they have already done this work for you (as well as a few more methods). Here are the extension methods for the String class that are provided to you:
| Method |
Description |
| endsWith() |
Test to see if the string ends with a particular string |
| format() |
Similar to .Net's String.Format(), it replaces a set of tokens with some replacement values |
| localeFormat() |
Similar to format(), but it uses the current culture to format dates and numbers |
| startsWith() |
Test to see if the string begins with a particular string |
| trim() |
Strips both leading and trailing whitespace from the string |
| trimEnd() |
Strips whitespace from the end of the string |
| trimStart() |
Strips leading whitespace from the string |
One other nice thing it provides is a StringBuilder class similar to .Net's System.Text.StringBuilder class. This is especially nice when creating big chunks of HTML to throw in an innerHTML property. <input type="button" name="btn1" value="Click Me!" onclick="javascript:changeText();"><br /> <span id="myText"></span> <script type="text/javascript">
function changeText() { var sb = new Sys.StringBuilder(); // thank you, ASP.Net Ajax! sb.append('<h1>How cool is this?</h1>'); sb.append('<br />'); sb.append('You just clicked my button. '); sb.append('Now we will change some text');
$get('myText').innerHTML = sb.toString(); }
</script>
Saturday, May 17, 2008 10:32:30 PM (Eastern Standard Time, UTC-05:00) Ajax | ASP.Net | Javascript
 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.
Wednesday, May 14, 2008 12:43:26 AM (Eastern Standard Time, UTC-05:00) .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 ("").
Tuesday, May 13, 2008 11:07:44 AM (Eastern Standard Time, UTC-05:00) 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.
Monday, May 12, 2008 4:45:45 PM (Eastern Standard Time, UTC-05:00) .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...
Thursday, May 08, 2008 11:21:04 PM (Eastern Standard Time, UTC-05:00) Cornell | Humor | TV
|

Subscribe to this feed
 Email Me
 Follow Me On Twitter
Search
Navigation
Tag Cloud
Archive
Blogroll
|