Painless Free Annual Credit Reports

There is finally a (relatively) painless way to receive your Free Annual Credit Report from the Big 3 Credit Agencies.  It is a really good idea to keep your eye on your credit report these days.  The folks at AnnualCreditReport.com have integrated the registration processes for the three Credit Agencies into a seamless wizard type experience.  I was able to print all three of mine in less than 10 minutes.

Posted in Random | Comments Off

Weather Widget now in the Silverlight.net Showcase

The Silverlight Weather Widget made it to the Silverlight.net Showcase and is mentioned in the News section of the Home Page.  WooHooo.

Posted in Silverlight 1.0 | Comments Off

The New Generic Kid on the Block–HashSet<T>

In mathematics, a Set is typically thought of as a collection of distinct objects that is usually defined by some rule that determines whether they are a member of that particular Set.  For example, a Set could be defined to contain "all the odd numbers under 100" or "every number divisible by 2" or whatever.  The main points here being that the objects in a Set are distinct (i.e. NO duplicate objects are allowed) and the objects are not ordered or sorted in any way.  (If you really feel the need to geek out on Set Theory, be my guest).

According to the MSDN Documentation for HashSet, "a set is a collection that contains no duplicate elements, and whose elements are in no particular order."  Sound familiar?  In the .NET Base Class Library, there has never been a true Set class until now with the release of .NET 3.5 and the brand-spanking-new System.Collections.Generic.HashSet<T> class.

In the past, when we needed to implement a Set in .NET, we sort of bastardized the List<T> or other unsuspecting Collection classes into something like this:

  1. int[] intArray = new int[]{ 1, 2, 5, 7, 4, 1, 7, 9, 8};
  2. List<int> intList = new List<int>();
  3. foreach(int theInt in intArray)
  4. {
  5.   // checking to make sure that the object is not in our List before adding
  6.   //  so we are calling Contains() and then Add() on every time through the loop
  7.   if (!intList.Contains(theInt))
  8.     intList.Add(theInt);
  9. }

What we needed was a Set that would let us just keep adding objects to it and free us from having to worry about it being there first.  A recent example of when I really needed a true Set class was while working on the Weather WidgetThe Weather Channel provides some 40+ images for use in their SDK, yet in the Weather Widget I never need more than maximum of 11 (and usually less as there is usually duplication).  So, I had the idea to dynamically zip the images that I actually needed for a given Zip Code and use that file for my image assets in Silverlight via CreateXamlFromDownloader. Obviously, I don’t want these images duplicated in the zip file.  So, I came up with this:

  1. HashSet<string> assets = new HashSet<string>();
  2. foreach (ForecastDay day in forecast.Days)
  3. {
  4.   // no Exception here if duplicate…it just doesn't add it.
  5.   assets.Add(Server.MapPath(String.Format("~/images/{0}", day.IconDay)));
  6. }
  7. Utility.WriteZipFile(assets.ToArray<string>());

This just scratches the surface of what HashSet<T> is capable of.  There are member methods available for most Set operations, such as IntersectWith(), UnionWith(), IsSubsetOf(), IsSupersetOf(), RemoveWhere(), etc.   Here’s a link to all of the HashSet<T> Members.

Recall that being a member of a set depends on some rule that determines this membership.  With the HashSet<T>, you can define your own rule of what it means to be in a Set.  For a good example of defining your own EqualityComparer, see Introducing HashSet<T> from Kim Hamilton on the BCL Team Blog.

So, welcome the New Kid on the Block to the Generic Collections.

One last random link I had on the subject for those interested in LINQ:

Good side-by-side comparison of the HashSet and LINQ Set Operations

Posted in Generics | Comments Off

A Better Way to Remove a Trailing Slash from a Path

How many times have you written this to trim a trailing slash from a path:

  1. if (myPath.EndsWith("\\"))   /* or "/" in the case of a URI  */
  2.   myPath = myPath.Substring(0, myPath.Length-1);

Next time, try this:

  1. myPath = myPath.TrimEnd(new char[]{'\\', '/'});

In addition to String.TrimStart() and String.TrimEnd(), there is an overload on String.Trim() that accepts a character array. There are good examples of usage in the MSDN Library. Here
are the links:

System.String Trim(char[]) TrimStart(char[]) TrimEnd(char[])

Posted in C# | Comments Off

My First Look at the (Silver)Light

Over the holidays, I was finally able to catch up on some reading and take some time to play with Silverlight 1.0.  The two books that I spent the most time with are

ASP.NET Ajax in Action and Silverlight 1.0 Unleashed.  Both of these, I HIGHLY recommend!

My first project is a Silverlight 1.0 application that I call Weather Widget (for lack of a more exciting moniker).  The Weather Widget will accept a 5-digit US Zip Code and return a 5-day forecast from The Weather Channel.   You can see it in action here: 

WeatherWidget.  (Note:  This will require the Silverlight 1.0 plug-in).

I also took the time in this project to use (experiment) with a lot of new toys such as: 

·         LINQ to XML (and the XDocument/XElement classes)

·         New C# language enhancements including Automatic Properties, Object Initializers, Implicitly
typed variables, etc.

·         Javascript key events – some hard-learned (and forgotten and now painfully re-learned) lessons
to share here

·         Integration of ASP.NET Ajax and Silverlight – my implementation here is rudimentary right now as I’m still sorting through the most efficient blending of these two technologies

·         Integration of DOM Elements with Silverlight (good post from Keith on that topic here)

·         Expression Studio (mostly Blend) –  I have much to say about this (some of which you can read in this discussion in Shawn’s comments here).  Overall, I really like a lot of things about Blend.  I am finding myself there more these days though I miss a lot of things about working in Macromedia (now Adobe) Fireworks (which I have used for MANY years for all of my design work).  One upcoming post I’m working on here is “Top 10 Things I miss from Fireworks while working in Blend”.

·         Using JSON – I had never used it so now I have

 

 

Guess that about covers it. I’ll link back to the above points as I blog about each.

I have several things I’m planning to add (and am open to suggestions), but wanted to get it out there in its current state.  Once I am happy with it, I’ll post it to the gallery on the Silverlight.net site.

Let me know if the Widget blows up…at this point, I’ve tested only on IE7 and Firefox on my laptop (Windows Vista64). 

 

Posted in ASP.NET Ajax, Expression, Silverlight 1.0 | Comments Off

Sharing Folders in Remote Desktop

Today, I was struggling with getting some code deployed to a client’s test server and called him to double-check FTP credentials and such.   He casually mentions having the ability to share your hard drive (and other devices) with a Remote Desktop session.  How many times would that have come in handy?!?!  Anyway, I dug into it further and it works!

Check out Dan Mork’s blog posting on the topic as he does a great job of walking you through the process.  (Make sure and check the comments as there is another really good tip there about saving your Remote Desktop connection with your configuration).

Posted in Windows Tips | Comments Off

Deep Dive: CSS for the ASP.NET Developer

Thanks everyone for the comments and tips offered after I gave this presentation at Alabama Code Camp, as well as, at the Atlanta Cutting Edge .NET group last week. I received several "so that’s how it works" comments and that was exactly the point of this talk.

Here is the abstract of the presentation:

This will be a thorough discussion of all that is CSS.  Whether you know it as the necessary evil or the great enabler (that just hasn’t quite clicked for you yet), you should walk away with something valuable from this discussion.  I will begin with the basic box model and travel all the way to the holiest of grails (the no tables here, two and three column ASP.NET Master Page layout…yours to take home for free!).  Along the way, we’ll touch on some CSS Best Practices and gotchas in ASP.NET and take a look at the new CSS tools in Visual Studio 2008 (Orcas).

Get the download here.

Note: The solution file provided is from Visual Studio 2008 (Orcas) Beta 2.  There
is nothing .NET 3.5 specific (as most of it is HTML anyway). 
 

Update 04/01/08:  Updated the download to compile under Visual Studio 2008 RTM

Posted in CSS, Presentations | Comments Off

Time for T : An Introduction to .NET Generics

Finally able to grab a minute to post my code and slides from the Introduction to Generics presentation that I did this past weekend at the Alabama Code Camp at the University of Alabama.

This is the abstract from the presentation:

With the release of the 2.0 version of the .NET Framework, Generics became first class citizens in the Common Language Runtime.  Yet, many still shy away from using them because of perceived difficulty or other misconceptions.   This presentation will seek to dispel a few of these myths and offer a gentle introduction into using Generics on a daily basis.  Along the way, I’ll also demonstrate language enhancements in the .NET 3.5 runtime that lend themselves nicely to working with Generics. 

Get the download here

Note:  The code is compiled on Visual Studio 2008 (Orcas) Beta 2.  The only project in the solution that uses .NET 3.5 specific code is the Collections project.  This uses C# 3.5 Automatic Properties and Property Initializers. 

Posted in Generics, Presentations | Comments Off

Generic Methods: Find Controls by Type

Although this was originally part of my recent Generics presentation, I have received several requests to publish it separately.

The reason that I created this originally was that I found myself often-times wanting a strongly-typed list of all the checkboxes / buttons / etc in my code-behind/beside pages.  There is the Page.FindControl(string id), but that only allows you to get a control by id and it returns a Control.  I wanted something more specific, yet generic enough to use as a Utility.  This was screaming for Generic Methods, so below is what I came up with.

 

I use this constantly and hope that someone else gets some mileage out of it.  If you have improvements, please post them.  For example usage, download the full Generics presentation.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Controls;
  4.  
  5. namespace r2musings.Web.UI.WebControls
  6. {
  7.     public static class Utility
  8.     {
  9.         #region FindControlsByType
  10.         /// <summary>
  11.         ///   Returns a generic list of controls of a provided type starting at a
  12.         ///    a provided base control (works recursively)
  13.         ///    Example Usage:  
  14.         ///       List<Button> buttonList = Utility.FindControlsByType<Button>(testPanel);
  15.         ///       This would return a list of all Buttons contained anywhere within testPanel
  16.         /// </summary>
  17.         /// <typeparam name="T">Type of control</typeparam>
  18.         /// <param name="parentControl">Base control to start search</param>
  19.         /// <returns></returns>
  20.         public static List<T> FindControlsByType<T>(Control parentControl)
  21.                     where T: System.Web.UI.Control
  22.         {
  23.             // new up our return list
  24.             List<T> returnList = new List<T>();
  25.  
  26.             // loop through all controls and call internal recursion to
  27.             //   add all controls of type T to the returnList
  28.             foreach (Control childControl in parentControl.Controls)
  29.             {
  30.                 InternalFindControlsByType<T>(childControl, returnList);
  31.             }
  32.  
  33.             // return our List<T>
  34.             return returnList;
  35.         }
  36.         
  37.         #endregion
  38.  
  39.         #region Recursion Method
  40.         
  41.         /// <summary>
  42.         ///   Should NOT call this method directly
  43.         ///   This is for the internal recursion of FindControlsByType()  
  44.         /// </summary>
  45.         /// <typeparam name="T">Type of control</typeparam>
  46.         /// <param name="parentControl">Base control to start search</param>
  47.         /// <param name="returnList">List to add Controls</param>
  48.         private static void InternalFindControlsByType<T>(
  49.             Control parentControl, List<T> returnList)
  50.             where T: System.Web.UI.Control
  51.         {
  52.             if (returnList == null)
  53.                 throw new ArgumentNullException("Null List passed to InternalFindControlsByType");
  54.  
  55.             if (parentControl is T)
  56.             {
  57.                 returnList.Add((T) parentControl);
  58.             }
  59.  
  60.             foreach (Control childControl in parentControl.Controls)
  61.             {
  62.                 // call this method recursively to get all child controls
  63.                 InternalFindControlsByType(childControl, returnList);
  64.             }
  65.         }
  66.  
  67.         #endregion
  68.     }
  69. }

Posted in Generics | Comments Off

Google, Live, MSDN Search within Visual Studio 2008

Since Matt Ranlett has now harassed me several times for removing the Google/MSDN macro from my blog, I decided to repost it.  I have added Live Search to the mix this time and I have tested all of the searches on Visual Studio 2008 Beta 2.

The background on this was that I was spending a lot of time on Google and MSDN (aren’t we all?) and was going back and forth from Visual Studio to the browser, so I decided to try and integrate my searches into Visual Studio.  The resultant experience is that you can highlight any text in Visual Studio and press your assigned keyboard shortcut and receive search results from your favorite search engines WITHIN the Visual Studio IDE.  It really comes in handy! 

In the code below, I have provided the URLs for searching against Google, Live, and MSDN.  It should be pretty obvious when you look at the macro what you would need to do to configure any other search engines.  Be sure to uncomment one (and only one) of the

url lines in the code when you setup each macro. 
 

For those not familiar with macros, here are the steps to add a macro in Visual Studio:

 

1)      Goto Tools | Macros | Macros IDE

2)      Once in the Macros IDE, you can create a new Module or you can just add this to the default “My Macros” module.

3)      To add to My Macros, just right click on it in the Project Explorer and choose Add | Add New Item.  (if you don’t see the Project Explorer,
try
View | Project Explorer).

4)      When the Add New Item Dialog shows up, select Code File and enter GoogleSearch and click OK.

5)      Paste the Code for GoogleSearch below into this file and save it. Make
sure that you uncomment one of the url lines in the code.

6)      Click Debug | Build and then close the Macros IDE.

7)      Once back in VS2005, click Tools | Customize and click the Keyboard button at the bottom of the Customize Dialog.

8)       In the Show Commands Containing box, type “macros” and look for an entry that looks like this: “Macros.MyMacros.GoogleSearch.GoogleSearch”.  (This could be different depending on where you created the macro).

9)      Once you have the correct macro selected, go to the Press Shortcut Keys box and type whatever keys you want to fire the macro (I used Alt-G for Google, Alt-M for MSDN, Alt-L for Live).

10)  Click the Assign button and then OK and then finally Close back on the Customize Dialog.

11) Highlight any text in the IDE and fire off your shortcut key to test.

12) Repeat for each search macro you want to setup. 

Here is the final code for the macro:

 

  1. Imports EnvDTE
  2. Imports System.Text.RegularExpressions
  3. Public Module GoogleSearch
  4.     Sub GoogleSearch()
  5.         Dim url As String
  6.         Dim selectedText As TextSelection = DTE.ActiveDocument.Selection()
  7.         If Not String.IsNullOrEmpty(selectedText.Text) Then
  8.             ' uncomment below for Google
  9.             ' url = String.Format("www.google.com/search?q={0}",
  10.             '        Regex.Replace(selectedText.Text, "\s{1,}", "+"))
  11.             ' uncomment below for MSDN search
  12.             ' url = String.Format("http://search.msdn.microsoft.com/search/Default.aspx?brand=msdn&query={0}",
  13.             '        Regex.Replace(selectedText.Text, "\s{1,}", "+"))
  14.             ' uncomment below for Live search
  15.             ' url = String.Format("http://search.live.com/results.aspx?q={0}",
  16.             '        Regex.Replace(selectedText.Text, "\s{1,}", "+"))
  17.             DTE.ExecuteCommand("View.WebBrowser", url)
  18.         Else
  19.             MsgBox("No text selected.")
  20.         End If
  21.     End Sub
  22. End Module

Posted in Macros, Visual Studio 2005, Visual Studio 2008 | Comments Off