1

I'm trying create pagination on my page. The user can select the number of items that will appear per page, the preferred size then will be saved as cookie. But when I try to choose between the querystring parameter and cookie, an error occured:

    public ActionResult Index(string keyword, int? page, int? size)
    {
        keyword = keyword ?? "";
        page = page ?? 1;

        //Operator "??" cannot be applied to operands of type "int" and  "int"
        size = size ?? Convert.ToInt32(Request.Cookies.Get("StoriesPageSize").Value) ?? 10; 

What's causing this error? How to fix it?

1

2 Answers 2

6

Convert.ToInt32 just returns int, not int? - so the type of the expression:

size ?? Convert.ToInt32(...)

is of type int. You can't use a non-nullable value type as the first operand of a null-coalescing operator expression - it can't possibly be null, so the second operand (10 in this case) could never possibly be used.

If you're trying to try to use the StoriesPageSize cookie, but you don't know whether or not it's present, you could use:

public ActionResult Index(string keyword, int? page, int? size)
{
    keyword = keyword ?? "";
    page = page ?? 1;

    size = size ?? GetSizeFromCookie() ?? 10;
}

private int? GetSizeFromCookie()
{
    string cookieValue = Request.Cookies.Get("StoriesPageSize").Value;
    if (cookieValue == null)
    {
        return null;
    }
    int size;
    if (int.TryParse(cookieValue, CultureInfo.InvariantCulture, out size))
    {
        return size;
    }
    // Couldn't parse...
    return null;
}

As mentioned in comments, you could write an extension method to make this more generally available:

public static int? GetInt32OrNull(this CookieCollection cookies, string name)
{
    if (cookies == null)
    {
        throw ArgumentNullException("cookies");
    }
    if (name == null)
    {
        throw ArgumentNullException("name");
    }
    string cookieValue = cookies.Get(name).Value;
    if (cookieValue == null)
    {
        return null;
    }
    int size;
    if (int.TryParse(cookieValue, CultureInfo.InvariantCulture, out size))
    {
        return size;
    }
    // Couldn't parse...
    return null;
}

Note that I've changed the code to use the invariant culture - it makes sense to propagate information in cookies in the invariant culture, as it's not really meant to be user-visible or culture-sensitive. You should make sure you save the cookie using the invariant culture too.

Anyway, with the extension method in place (in a static non-generic top-level class), you can use:

size = size ?? Request.Cookies.GetInt32OrNull("StoriesPageSize") ?? 10;
Sign up to request clarification or add additional context in comments.

5 Comments

you're throwing away sizeFromCookie
Another function? :( Is there any overload in convert.ToInt32 or the like that allows a nullable int as result?
@domanokz: Not that I'm aware of - and bear in mind there are two different cases you need to take into account: there being no matching cookie, and it not having a parseable value. You could easily write a general purpose method (possibly an extension method on CookieCollection) to return int? though.
Woah! It's Jon Skeet! O_O Anyway, yeah you're right, I could be getting Value from null. Thanks!
Yeah it was just for the readability, especially since your rep means that people might read all sorts of things into your code (I know I always check a few extra times before giving comments like the above)
1

The problem is that the result of the first operation (size ?? Convert.ToInt32(Request.Cookies.Get("StoriesPageSize").Value)) is an int. Then you use the Null Coalescing Operator between this int and another int, but since an int cannot be null, it fails.

It does not have a meaning to use the Null Coalescing Operator if the left hand side cannot be null, so the compiler gives an error.

Regarding how to fix it, you can rewrite it like this:

size = size ?? (Request.Cookies.Get("StoriesPageSize") != null ? Convert.ToInt32(Request.Cookies.Get("StoriesPageSize").Value) : 10);

1 Comment

Better wrap that in a Try/Catch, you have the potential of throwing a FormatException, InvalidCastException or OverflowException

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.