Blog ( Of Ryan Olshan)

Recent Posts

News

About Ryan Olshan

LinkedIn Profile

Follow Ryan via twitter

Join StrongTypes - Free Generics Help By Email
Join StrongTypes

Community Credit Winner

View my MVP profile

The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

Subscribe to Rolog via My AOL
Subscribe to Rolog via Bloglines


Subscribe to Rolog via Feedburner

Furl Rolog




Subscribe to Rolog via Newsburst


Subscribe to Rolog via Pluck

Add Rolog to Technorati Favorites




Thycotic Secret Server

Receive Email Updates


.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • LearnAsp.com
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Charles Carroll
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Doug Nelson
  • Kevin Jones

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

A better way to parse query string values for non-string data types

Occasionally, I've glanced at the TryParse method in IntelliSense, but never gave it a test run. TryParse is actually pretty cool. It will try to parse a string and see if it can convert it to a specified data type. This is perfect for query strings as you don't have to mess with testing for null values before either parsing or converting the query string to a data type and also eliminates any runtime errors that may be thrown and/or having to catch them.

string  booleanQueryString = Request.QueryString[ "SomeBoolean" ];
string  doubleQueryString = Request.QueryString[ "SomeDouble" ];
string  integerQueryString = Request.QueryString[ "SomeInteger" ];

double doubleValue;
int
integerValue;
bool booleanValue, booleanParseValue, doubleParseValue, integerParseValue;

booleanParseValue = bool .TryParse(booleanQueryString, booleanValue);
doubleParseValue = double .TryParse(doubleQueryString, doubleValue);
integerParseValue = int .TryParse(integerQueryString, integerValue);

if (booleanParseValue)
{
    // Do something
}

if (doubleParseValue)
{
    // Do something
}

if (integerParseValue)
{
    // Do something
}

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Jan 22 2006, 01:10 PM by Ryan Olshan | with no comments
Filed under:

Comments

No Comments