Over the past couple of weeks I've been working on a .NET v1.1 web application. Most of the pages of the application were being passed query strings, which where used throughout methods of the page by calling code similar to this....
int id;
if (Request.QueryString["id"] == null)
{
id = int.Parse(Request.QueryString["id"]);
}The above code was being used in a number of methods throughout the page, everytime 'id' was needed the code was used, which resulted in quite a lot of code duplication and bloat. I've not worked on many web applications so I'm not sure how many developers do the same thing. If your are doing this then maybe creating a read only property for the query string might be better. For example:
public int EmployeeID
{
get
{
if (Request.QueryString["employeeId"] != null)
{
return int.Parse(Request.QueryString["employeeId"]);
}
else
{
return 0;
}
}
}The methods that need the 'employeeId' query string can be reduced to 'this.EmployeeID', for example:
getEmployee(this.EmployeeId);
This approach has a load of advantages, you could decode/decrypt/validate/format the query string in the property, it saves a little bit of memory, and it makes code much more readable.
*NOTE: you might need to use the fully quantifed HttpContext.Current.Request.QueryString.