I use this simple method to retrieve a Query String from a Page a returning a Int. In case a valid Id can not be found I would like return a default value int 0.
I would like to know if there is a better way to write this method, in details:
- Would be better use just a single return statement instead the two? If yes what is the best way to do it?
- Shall I perform any other checking when retrieving the Query String?
protected int GetPageIndex()
{
int output;
bool result = Int32.TryParse(Request.QueryString["page"], out output);
if(result)
return output;
return output = 0; // Return Default value
}
Just initialize it before you try to parse it… Anyways, Int32.TryParse sets the value to 0 if the conversion is unsuccessful… 🙂 http://msdn.microsoft.com/en-us/library/f02979c7.aspx