Home > Paul Busby, Work > How to validate string.format within Visual Studio

How to validate string.format within Visual Studio


In writing code we use string.format a lot to create strings for error reporting or sending to the screen. Some of these will only ever get created if an error occurs so they become difficult to test to make sure the don’t throw an error. Take the following example

The following code would fail at runtime but is fine in the compiler

string.Format("{0} This is valid but will error {1}", this.siteURL);

So how do we catch these. Well we all use the code analysis tools built into visual studio. Don’t we?


Well this has a nice test case that will catch and highlight this problem.


Run code analysis on your project Alt+F11 or select “Run Code Analysis on …” from the Analyse menu.


Then once this has finished you can search for issue CA2241. This will show any and all string.formats that would fail at run time.


It would be nice if the compiler caught these but this is still a good approach.

via Buzz Blog http://bit.ly/1hYG3FD

Chris Stretton
Paul is a an expert SharePoint and Project Server developer and is responsible for designing and implementing custom solutions on client systems using the latest SharePoint and .NET technologies.
Paul has extensive experience with SharePoint systems across all sizes of implementation, ranging from small to large farms and has an excellent understanding of all the elements of SharePoint.

This article has been cross posted from paulbuzzblog.wordpress.com (original article)

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: