Wednesday, October 7, 2009

"Add Reference" Dialog Delays

Today I decided to reduce the impact of an annoying problem with Visual Studio--the length of time it takes to initially bring up the "Add Reference" dialog. It can take a minute! I run into this delay constantly because I seem to almost always need to add a reference to System.Configuration to every project. My approach was to create a custom project template with the reference already included. Here are the steps I took:
  • Navigate to "C:\program files\microsoft visual studio 9.0\common7\IDE\ProjectTemplates\CSharp\Windows\1033"
  • Copied the template ConsoleApplication.zip
  • Navigate to My Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual C#
  • Pasted ConsoleApplication.zip
  • Opened up the zip with "Open with > Compressed (zipped) folders" and dragged "consoleapplication.csproj" onto the desktop
  • At line 45 added
  •     <Reference Include="System.Configuration" />
  • Saved the proj file
  • Opened up the zip with Winzip (or use 7zip) and dragged in the new version of "consoleapplication.csproj"
  • Closed winzip (or 7zip)
The new project template is ready to use!

The official documentation from Microsoft is here.

Tuesday, October 6, 2009

Executing Console Commands with PowerShell

I'm just starting to master PowerShell . . and I emphasize the word "starting".  Frankly, I'm disappointed with the learning curve required. PowerShell doesn't seem to be optimized, yet, for quick learning, despite that it piggy-backs off of .NET.

Here's a tip. Don't throw away those useful scraps of "DOS" commands you've been carrying around for many years.  You can reuse them inside of PowerShell. Here's an example:

 cmd /c  <PathToCommand> arg0 arg1 argEtc

Getting Started with the 7Zip Command Line

7Zip's command line tool has umpty-um options. Many of them are very useful. But if you are looking only for a command line version of the Windows' "Send to > Compressed (zipped) Folder" command, then you can get by with merely the following:

 7z a -tzip <ZippedFileName>.zip <FolderName>

Thursday, October 1, 2009

Using C Sample Code in MSDN

Here's a simple tip for Windows C (and C++) programmers. There's a wealth of C code samples in MSDN. Unfortunately, many of those I've seen and used suffer from the same deficiency: they not unicode friendly and therefore not compatible with recent versions of Visual Studio. It's relatively easy to fix, however. Substitute the _tmain function for main and for _tmain's second parameter use "_TCHAR* argv[]" instead of "char *argv[]."

Wednesday, September 16, 2009

GOTO May Be Considered Helpful

As it turns out, at least one legitimate use exists for the notorious GOTO statement: breaking out of nested loops. Knowing from first-hand experience the problems that could be caused by "goto programming", I hadn't written a goto in 25 years. Recently, however, when trying to a write function with nested loops, it dawned on me that the most elegant solution would indeed involve a goto, at least when using C#. In fact, Microsoft's C# reference guide specifically recommends using a "goto" when exiting an inner loop, I later found out. (Maybe I ought to be reading Microsoft's online docs more often.)

In reviewing Steve McConnell's Code Complete (Second Edition), as I always do when confronting fundamental coding challenges, I was surprised to find only the vaguest of advice regarding "goto"--never say never, use your head, etc. Earlier in his book, McConnell does mention a Java-only approach. Java, which doesn't implement goto, actually has the nifty solution of named blocks, allowing you to end a break statement with the name of a block. I think this is superior than using a goto, but Java seems to be the only language with this feature.

Another author I consulted in this regard was Jeff Atwood. Atwood also describes beneficial situations for using goto, but in the only in the context of an "early return," rather than as a solution specifically for exiting nested loops. On the other hand,  I really like Atwood's description of "return" and "exit" as being merely tightly scoped goto's.

Thursday, September 10, 2009

Loading Image Resources

Here's sample code to load an image resource file, which works even if the resource file is not in the executable assembly, but in a referenced assembly.

Bitmap bmp = new Bitmap(

    System.Reflection.Assembly.GetAssembly(typeof(pick_a_class)).

    GetManifestResourceStream("Dll.Path.Image.jpg"));


pick_a_class is the name of any class in the relevant assembly.

The resource name may not be obvious. Here's code to determine the exact names of the various resources:

string[] all = System.Reflection.Assembly.GetAssembly(typeof(pick_a_class)).

    GetManifestResourceNames();

Tuesday, September 8, 2009

Manipulating Xml with C#

I'm pretty fond of the XDocument class. XDocument lets you have your way with XML without requiring convoluted syntax, especially when used in conjunction with lambda expressions. Here's an example to change the value of an attribute on nodes that were found through a search criterion.


XDocument xDoc = XDocument.Load(

    new StringReader(

        @"<Customers><Customer ID=""99"" Description=""Just another customer""></Customer></Customers>"));

xDoc.Descendants("Customer").ToList().

    ForEach

    (e =>

        {

            if (e.Attribute("ID").Value == "99")

            { e.Attribute("Description").Value = "Our best customer"; }

        }

    );