-1

I modified ASP.NET Identity 2.0 Extending Identity Models in order to use int keys instead of string (GUID) as indicated on ASP.NET Identity 2.0 Extending Identity Models and Using Integer Keys Instead of Strings and modified the logic on some methods methods as shown below:

public async Task<ActionResult> Details(string id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    var user = await UserManager.FindByIdAsync(id);
    ViewBag.RoleNames = await UserManager.GetRolesAsync(user.Id);
    return View(user);
}

to this:

public async Task<ActionResult> Details(int id)
{
    if (id > 0)
    {
        // Process normally:
        var user = await UserManager.FindByIdAsync(id);
        ViewBag.RoleNames = await UserManager.GetRolesAsync(user.Id);
        return View(user);
    }
    // Return Error:
    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}

On the other hand, instead of changing logic, I just want to change the condition in if block, but I am not sure what is the best way to check null values for int?

The original method is like that:

if (id == null) //id is string

As I had to change string to int type, I need to check null something like that:

if (id !> 0) //id is int 

Any idea?

Jack
  • 1
  • 21
  • 118
  • 236
  • Int cannot be a null, your best bet is to use -1 as a null value (assuming it will never be used normally) – Mark Phillips Oct 25 '16 at 08:58
  • 1
    `If (id == 0)`. But better to make it `int?` (nullable) so that you can use `if (!id.HasValue)` –  Oct 25 '16 at 08:59
  • This question is unclear. The method you show is an action method, so it's called through MVC. What intended use cases do you have? What error handling do you have in place? What happens when a user omits the `id` parameter when calling this action method? What do you want to happen when they request the page with `id` set to `0`? What are you really trying to do? – CodeCaster Oct 25 '16 at 09:01
  • @StephenMuecke I should not use nullable int as the id parameter also dependent. So, it ok using **if (id == 0)** instead of **if (id == null)**? Thanks... – Jack Oct 25 '16 at 09:03
  • @PatrickHofman **Not duplicate** as requiring non-nullable. Any idea to fix it? – Jack Oct 25 '16 at 09:07
  • @StephenMuecke Any idea regarding to the last comment sent to you? – Jack Oct 25 '16 at 09:08
  • You can't make something that isn't nullable `null`. The key point of your question is: how to allow the value to be null. The dup answers that. – Patrick Hofman Oct 25 '16 at 09:11
  • 1
    Why not make it nullable? But I assume you wont have records in your database with `id = 0` so it should be OK. –  Oct 25 '16 at 09:11
  • @StephenMuecke Because using int? causes "cannot convert from int to nullable int..." error and I do not want to modify the related class parameters. – Jack Oct 25 '16 at 09:13
  • @StephenMuecke Thanks a lot, I deciden to use if (id == null). – Jack Oct 25 '16 at 09:14
  • Then use `id.Value` for the places you need an int. – Patrick Hofman Oct 25 '16 at 09:16

1 Answers1

1

A normal integer cannot be null, the default value is 0. If you want a nullable integer you need to use int? Then you can check if it's null or 0 like so:

if (id == null) //!id.HasValue
{
    //some stuff
} else if(id == 0) {
    //other stuff
}
Leon Husmann
  • 664
  • 1
  • 6
  • 25