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 June, 2010

Difference betweenTryParse and Convert in C#

TryParse

int index;
int.TryParse(Request.QueryString["index"], out index);

This version isn’t so bad since it requires no conditional processing. The problem with the TryParse method is the out parameter, which requires the initial declaration of variable whether or not you want or need it. This also prevents it from being used in a declarative statement, since the return value is whether the value was parsed. This can be used declaratively using the ternary operator, but it is rather odd since the result is already defined. Many people opt to use imperative logic instead, testing for !<t>.TryParse.

Here’s are examples of non-zero default value. The first using an if statement and the second with the ternary operator.

int index;
if (!int.TryParse(Request.QueryString["index"], out index))
{
    index = 1;
}
int index;
index = int.TryParse(Request.QueryString["index"], outindex) ? index : 1;

Convert

var index = Convert.ToInt32(Request.QueryString["index"]);

Convert is more declarative in this form, but it has a fatal flaw. If the string being parsed is an invalid value (not null, but something like “asdf”), then it will throw an exception. Exceptions are expensive, and it requires wrapping the code in a try… catch block since the exception is typically meaningless aside from preventing the assignment. Perhaps the above statement would be better expressed as:

int index = 0;
try
{
    index = Convert.ToInt32(Request.QueryString["index"]);
}
catch { }

The typical conversion failure use case is to assign a default value. However, additional logic is sometimes necessary. With TryParse, you test the return value for false. With Convert, you catch the exception. With Maybe, you test for null.

int? result = Maybe.ToInt32(Request.QueryString["index"]); if (result == null) { // Failure processing }

Since it is sometimes necessary to know if a the parsing was successful, I didn’t opt to create a ParseWithDefault method.


Try Expression Web 4 now

Microsoft Expression Web 4 is a professional development and design tool to create modern, standards-based Web sites that use PHP, HTML/XHTML, CSS, JavaScript, ASP.NET or ASP.NET AJAX.
The highlight including:

  • Create standards-based Web sites faster & easier
  • Professional code editor & design surface
  • Make your site stand out with rich graphics support
  • Work with leading industry-standard technologies

What’s new in Expression Web

SuperPreview online service

The Microsoft Expression Web SuperPreview online service is a beta service that extends the capability of SuperPreview to include support for additional browsers and operating systems.

SEO Checker

The SEO Checker feature of Expression Web analyzes your site against the best practices for getting the highest possible search-engine rankings for your site.

You can choose SEO options, display an SEO report, filter the results in the SEO report, and step forward and back through the list of results in the SEO report to see more detail for individual list items.

You can try the 60-day free trail at http://www.microsoft.com/downloads/details.aspx?FamilyID=7b10272b-2f96-4250-aede-3fcfadf26f34

Visual Studio 2010 Pro Power Tools, It Is Free!

A set of extensions to Visual Studio Professional (and above) which improves developer productivity.

  • Searchable Add Reference Dialog
    The new Add Reference dialog makes it faster and easier for you to find the reference that you are looking for and add it to your VB, C# or F# project.  From the Solution Explorer, simply right click on the References node, select the Add Reference command to see the updated Add Reference Dialog. 
  • Highlight Current Line
    As the resolution of monitors increases, it’s becoming more difficult to find the caret in the code editor.  The highlight current line extension makes it easy to find the caret by highlighting the line that the caret is on in the editor.  You can even configure the default colour by changing the setting for “Current Line (Extension)” and “Current Line Inactive (Extension)” in Tools Options Fonts & Colors. 
  • HTML Copy
    This extension provides support for the HTML Clipboard format when copying code from the editor.  This means that you’ll no longer have to go fix up the formatting of your code when you paste it into a TFS bug form or any other HTML based control. 
  • Triple Click
    It’s never been easier to select a line of code from the mouse by simple triple-clicking anywhere on the line. 
  • Fix Mixed Tabs
    Some developers prefer tabs, others prefer spaces, and nobody likes mixing tabs & spaces.  This extension promotes developer harmony by warning as they are open or save a file that has a mixture of tabs & spaces.  The information bar also provides an easy way to fix the file to suit your preference. 
  • Ctrl + Click Go To Definition
    This extension gives the editor a web browser by adding clickable hyperlinks to symbols in your code as you hold down the Ctrl key.
  • Colorized Parameter Help
    This extension improves consistency with the editor by applying syntax highlighting to the contents of the Parameter Help window for C# &VB. 
  • Move Line Up/Down Commands
    This extension maps the Alt+Up Arrow & Alt+Down Arrow keys such that they will move the current line of code or the selected lines up and down through the editor. 
  • Column Guides
    Since Visual Studio 2002, there has been a not so secret registry key which allowed user to draw a vertical line in the code editor.  This is very useful to remind developers that their full line of code or comments may not fit one a single screen. Thanks to this extension this feature has returned with UI configure it.  Simply place the cursor at the appropriate column and select Add Guideline from the context menu

Problems with an External (non-ASP.NET) Root Cause

Sometimes when you’re having trouble with an ASP.NET site, the problem turns out to not be ASP.NET itself. Here’s the top three issues and their causes. This category are for cases that were concluded because of external reasons and are outside of the control of support to directly affect. The sub categories are 3rd party software, Anti-virus software, Hardware, Virus attacks, DOS attacks, etc.

If you’ve ever run a production website you know there’s always that argument about whether to run anti-virus software in production. It’s not like anyone’s emailing viruses and saving them to production web servers, but you want to be careful. Sometimes IT or security insists on it. However, this means you’ll have software that is not your website software trying to access files at the same time your site is trying to access them.

Here’s the essence as a bulleted list

  • Concurrency while under pressure: This causes problems in big software. Make sure your anti-virus software is configure appropriately and that you’re aware of which processes are accessing which files, as well as how, why and when
  • Profile your applications: .NET and the Web are not black boxes. You can see what’s happening if you look. Know what bytes are going out the wire. Know who is accessing the disk. Measure twice, cut once, they say? I say measure a dozen times. You’d be surprised how often folks put an app in production and they’ve never once profiled it.
  • Anti-Virus Software: It can’t be emphasized enough that site owners should ensure they are running the latest AV engine and definitions from their chosen anti-malware vendor. They’ve see folks hitting hangs due to flakey AV drivers that are over two years out of date.  Another point about AV software is that it is not just about old-school AV scanning of file access. Many products now do low level monitoring of port activity, script activity within processes and memory allocation activity and do not always do these things 100% correctly. Stay up to date!
  • Know where you’re calling out to: Also, connection to remote endpoints: calling web services, accessing file systems etc. All of this can slow you down if you’re not paying attention. Is your DNS correct? Did you add your external hosts to a hosts file to remove DNS latency? 
  • processModel autoconfig=true: This is in machine.config and folks always mess with it. Don’t assume that you know better than the defaults. Everyone wants to change the defaults, add threads, remove threads, change the way the pool works because they think their textboxes-over-data application is special. Chances are it’s not, and you’d be surprised how often people will spend days on the phone with support and discover that the defaults were fine and they had changed them long ago and forgotten. Know what you’ve changed away from the defaults, and know why.