Jack @ ASP.NET

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries for April, 2010

Box Selection with Visual Studio 2010

Box selection is a feature that has been in Visual Studio for awhile (although not many people knew about it).  It allows you to select a rectangular region of text within the code editor by holding down the Alt key while selecting the text region with the mouse.  With VS 2008 you could then copy or delete the selected text.

VS 2010 now enables several more capabilities with box selection including:

  • Text Insertion: Typing with box selection now allows you to insert new text into every selected line
  • Paste/Replace: You can now paste the contents of one box selection into another and have the content flow correctly
  • Zero-Length Boxes: You can now make a vertical selection zero characters wide to create a multi-line insert point for new or copied text

These capabilities can be very useful in a variety of scenarios.  Some example scenarios: change access modifiers (private->public), adding comments to multiple lines, setting fields, or grouping multiple statements together.

A cool Visual Studio 2010 feature

Upgrade or Uninstall of Office 2007 might cause VS 2008 Web designer to hang

On machines with a 64-bit OS, uninstall of Office 2007 will break the VS 2008 web designer.  You will find that the VS designer won’t be able to load, and any attempts to switch to Design View will hang the product.  Note that 32-bit machines are not affected and neither are other VS releases.

The root cause of this problem is that Office 2007 and the VS 2008 web designer both share a component, 32-bit MSXML5.  When Office 2007 uninstalls or gets upgraded, MSXML5 is removed yet Visual Studio still needs it.

If you are affected by this issue, there are workarounds where you may not have to completely reinstall Visual Studio.  However, the steps are different depending on how Office 2007 got removed (uninstall vs. upgrade to Office 2010) and also if you have VWD Express or a full product.

Workaround if you manually uninstalled Office 2007 (i.e. Add/Remove Programs) AND have a non-Express version of VS 2008:

  1. Go to Add/Remove Programs
  2. Uninstall “Microsoft Visual Studio Web Authoring Component
  3. Reinstall “Microsoft Visual Studio Web Authoring Component” -
    • You will need the VS install media: the product DVD or mount an ISO image
    • Go to WCU\WebDesignerCore folder and run WebDesignerCore.exe.
    • Setup does not have UI, so please simply wait about 5 minutes or watch msiexec activity in the Task Manager.
    • Verify that Microsoft Visual Studio Web Authoring Component reappears in Add/Remove Programs.
    • WebDesignerCore.exe installs RTM version.
  4. If your VS 2008 has been updated to SP1, you have to reinstall SP1 to update the Web Authoring Component you installed in Step 3.

Workaround if you manually uninstalled Office 2007 (i.e. Add/Remove Programs) AND have Visual Web Developer Express 2008:

  1. Go to Add/Remove Programs  
  2. Uninstall “Microsoft Visual Studio Web Authoring Component
  3. Reinstall Visual Web Developer Express 2008/SP1
    (SP1 for Express is not a separate install thus the need to reinstall the entire product)

C# Code Function: Converting List of strings to string

=== Program that converts List (C#) ===

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> dogs = new List<string>(); // Create new list of strings
        dogs.Add("Aigi");                       // Add string 1
        dogs.Add("Spitz");                      // 2
        dogs.Add("Mastiff");                    // 3
        dogs.Add("Finnish Spitz");              // 4
        dogs.Add("Briard");                     // 5

        string dogCsv = string.Join(",", dogs.ToArray());
        Console.WriteLine(dogCsv);
    }
}

=== Output of the program ===

Aigi,Spitz,Mastiff,Finnish Spitz,Briard

ASP.NET 4 and Visual Studio 2010 Released

ASP.NET 4 and Visual Studio 2010 are now available. ASP.NET 4 and Visual Studio 2010 include lots of new features and improvements that enable you to easily build, deploy and manage great Web sites and applications.

Everything You Need to Build Better Websites

Visual Studio 2010

Visual Studio 2010 makes it easier to edit, search, and navigate code. Improved VB and C# Intellisense makes it even easier to find and use classes within the .NET Framework. Improved JavaScript IntelliSense enables better AJAX development. New code navigation and visualization features enable you to quickly find and navigate large projects and visualize dependencies across your code-base. Improved unit testing, debugging and profiling help support building robust applications.

ASP.NET Web Forms
With ASP.NET 4, Web Forms controls now render clean, semantically correct, and CSS friendly HTML markup. Built-in URL routing functionality allows you to expose clean, search engine friendly, URLs and increase the traffic to your Website. ViewState within applications is smaller and can now be more easily controlled. And more controls, including rich charting and data controls, are now built-into ASP.NET 4 and enable you to build applications even faster.

ASP.NET MVC
ASP.NET MVC 2 is now built-into VS 2010 and ASP.NET 4, and provides a great way to build web sites and applications using a model-view-controller based pattern. ASP.NET MVC 2 adds features to easily enable client and server validation logic, provides new strongly-typed HTML and UI-scaffolding helper methods, enables more modular/reusable applications, and facilitates a clean unit testing and TDD workflow with Visual Studio 2010.

Web Deployment
Visual Studio 2010 makes deploying your Websites easy. You can now publish your Websites and applications to a staging or production server from within Visual Studio itself. Visual Studio 2010 makes it easy to transfer all your files, code, configuration, database schema and data in one complete package. VS 2010 also makes it easy to manage separate web.config configuration files settings depending upon whether you are in debug, release, staging or production modes.

Optional Parameters in C# 4.0

The definition of a method, constructor, indexer, or delegate can specify that its parameters are required or that they are optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters.

Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value is used. Default values must be constants.

Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list are not supported. For example, in the following code, instance method ExampleMethod is defined with one required and two optional parameters.

E.g.

public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10)

The following call to ExampleMethod causes a compiler error, because an  argument is provided for the third parameter but not for the second.

//anExample.ExampleMethod(3,  ,4);

However, if you  know the name of the third parameter, you can use a named argument to  accomplish the task.

anExample.ExampleMethod(3, optionalint: 4);

IntelliSense uses brackets to  indicate optional parameters, as shown in the following illustration.
Optional parameters in ExampleMethod
Consider a standard scenario with method overloading or constructor chaining.  In C# we’d have several methods with different signatures where, in effect, we’re really just after default values. Let’s take the scenario where we’ve got a little helper class to send emails in our application. In some cases we want to CC the administrator to troubleshoot issues; in some cases we want rich HTML emails rather than plain text. We might set up our methods like this: 1:  public void SendMail(string toAddress, string bodyText) 2:  { 3:      this.SendMail(toAddress, bodyText, true); 4:  } 5:    6:  public void SendMail(string toAddress, string bodyText, bool ccAdministrator) 7:  { 8:      this.SendMail(toAddress, bodyText, ccAdministrator, false); 9:  } 10:    11:  public void SendMail(string toAddress, string bodyText, bool ccAdministrator, bool isBodyHtml) 12:  { 13:      // Full implementation here 14:  } This is pretty standard method overloading and we essentially are setting default values (true for CC the Admin, and false for HTML emails). With C# 4.0 we can now make the code more concise by only having to implement 1 method: 1:  public void SendMail(string toAddress, string bodyText, bool ccAdministrator = true, bool isBodyHtml = false) 2:  { 3:      // Full implementation here 4:  } However, you do have to take into account your scenario.  If you have a situation where you actually need to know if the consuming code provided a value then this isn’t a good option because if “true” comes in for the 3rd parameter, you don’t know if the consuming code actually set this explicitly or if it was simply the result of the default value.  But in typical scenarios like this, it’s not a big deal.  Cracking open Reflector and looking at the IL that the C# compiler is generating: 1:  .method public hidebysig instance void SendMail(string toAddress, string bodyText, [opt] bool ccAdministrator, [opt] bool isBodyHtml) cil managed 2:  { 3:      .param [3] = bool(true) 4:      .param [4] = bool(false) 5:      .maxstack 8 6:      L_0000: nop 7:      L_0001: ret 8:  } Which Reflectors translates to a C# equivalent of: 1:  public void SendMail(string toAddress, string bodyText, [Optional, DefaultParameterValue(true)] bool ccAdministrator, [Optional, DefaultParameterValue(false)] bool isBodyHtml) 2:  { 3:  }