Friday, October 26, 2007

Finally...a use for .NET partial classes

When .NET 2.0 was released, partial classes was recognized as one of the top four cool new features (along with generics, anonymous methods, and iterators). After developing with 2.0 for some time now, I haven’t found a good reason to use partial classes (besides the Page class in ASP.NET that is created for you automatically). I do however recognize its importance for Visual Studio 2005 and how the auto-generated code and our own Page classes are created. Besides that, I just haven’t ran into a situation where I really needed it.

Recently I wrote some code where I stood up and cheered partial classes. If you ever used the .NET xsd.exe utility in .NET 1.1 to create a strong-typed class for an XSD file, you know that the tool created this handy set of classes for you to access the elements of an XML file. This was a good thing, but I would always end up writing several separate utility classes to provide a layer on top of, or along side of, the auto-generated class from xsd.exe. Although tempted at times, I couldn’t modify the auto-generated class file since I might regenerate it at a later time (I always did).

Much to my glee, xsd.exe in .NET 2.0 now creates partial classes! This now allows me to write separate class files and add properties and methods to the auto-generated ones from xsd.exe. This provides a nice separation/coupling from the xsd.exe generated code. Excellent!

I see a pattern here…for things that auto-generate code, partial classes are a welcome addition to .NET.

Friday, October 19, 2007

DataFormatString not working in GridView

I often use the DataFormatString in the .NET 2.0 GridView control. It seems like the DataFormatString property on a bound column is ignored unless you also set the column's HtmlEncode property to false (the default value is true).

.NET ToString() formatting help

Here is a useful 2-page cheat sheet on the various ToString() formatting options in the .NET Framework languages.

Download Here

Wednesday, October 17, 2007

Clear Visual Studio "Recent Project" entries

Sometimes you want to clear all the "Recent Projects" from my Visual Studio Start Page. You can do this by removing registry values at the following locations in the registry:

To clear recent files:
HKCU\Software\Microsoft\VisualStudio\8.0\FileMRUList

To clear recent projects/solutions:
HKCU\Software\Microsoft\VisualStudio\8.0\ProjectMRUList

Saturday, October 13, 2007

Regex.Replace

From Brian Davis:

The String.Replace function has been a valuable tool for years, allowing programmers to change strings by replacing a specific substring with another substring. While this is usually enough, the Regex.Replace function goes a step (ok, maybe 10 steps) further. It allows replacement of text using regular expressions. Not only can you define the text to replace using a regular expression, you can define what to replace it with using a replacement string containing special constructs which identify portions of the mathed text. A common example is the representation of a name. Let's say you have a name in a string like "John Doe", but you would really like to have "Doe, John". You could accomplish this with a few lines of code, splitting the string on a space and constructing a new string using the elements, or you could do it in one simple line:

strInput = Regex.Replace(strInput,"(?\S+) (?\S+)","${last},${first}")

This is a simplified example with a simple expression (we don't allow names like John J. Doe, John Boy Doe, John Doe, Jr., etc.), but it shows how using the Regex.Replace function can take several lines of code down to just a few. If we were to write a more complex expression, it would still require just one line of code in our application, whereas we may have to add dozens of lines of code with If...Else blocks or Select Case statements to accomplish complicated parsing.

Here's a look at some of the constructs that can be used in a Regex replacement string:

$& - matched text
$_ - original source string
$` - text before match
$' - text after match
${group_name} - text matched by named group
$1, $2 - text matched by numbered group
$$ - the literal "$"

Tuesday, October 2, 2007

Creating a type of Type based on a string

I recently ran into a case where I had a string value like "System.String" and I needed to determine what type this represented, then assign that to a member of type Type.

Type t = Type.GetType("System.String");

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...