Question

i am currently looking for a way to set the amount of results i show depending on if a querystring has been set.

So basically, i first want to check to see if a querystring called pagesize is present. If that is set to a value of 10, then i need it to change a variable to 10. If it's not set, then the variable is set to 6.

Here's my current code:

var pageSize = 0;

if (Request.QueryString["pagesize"].AsInt = 10)
{
    pageSize = 10;
}
else
{
    pageSize = 6;
}

The error i'm getting back is "Cannot assign to 'AsInt' because it is a 'method group'"

Any ideas?

Was it helpful?

Solution

As the error suggests, the AsInt method is an extension method. Just add some brackets to call the method, also missing an extra =, as per the comments:

var pageSize = 0;

if (Request.QueryString["pagesize"].AsInt() == 10)
{
    pageSize = 10;
}
else
{
    pageSize = 6;
}

You can further simplify your code by taking advantage of the conditional operator. The following line achieves the same result:

var pageSize = Request.QueryString["pagesize"].AsInt() == 10 ? 10 : 6;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top