-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?

11
  • Int cannot be a null, your best bet is to use -1 as a null value (assuming it will never be used normally) Oct 25, 2016 at 8:58
  • 1
    If (id == 0). But better to make it int? (nullable) so that you can use if (!id.HasValue)
    – user3559349
    Oct 25, 2016 at 8: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, 2016 at 9: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, 2016 at 9:03
  • 1
    Why not make it nullable? But I assume you wont have records in your database with id = 0 so it should be OK.
    – user3559349
    Oct 25, 2016 at 9:11

1 Answer 1

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
}
6
  • I do not want to use nullable, is it possible to use this without nullable?
    – Jack
    Oct 25, 2016 at 8:59
  • 2
    @Clint are you asking "Can a non-nullable int be null"?
    – CodeCaster
    Oct 25, 2016 at 8:59
  • @ClintEastwood no that's not possible Oct 25, 2016 at 9:00
  • @CodeCaster Ok, updated.
    – Jack
    Oct 25, 2016 at 9:04
  • 1
    Sure you can check if your int is 0 or not. Oct 25, 2016 at 9:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.