Search code examples
c#goto

Bypass a labeled statement


I am currently implementing a goto statement in my code but upon debugging I notice that the labeled statement is also executed even I am not calling the goto statement. Is my code correct about implementing to goto statement? Thanks for any help.

Below is my code structure and the "InsertAdd" labeled statement is also executed even the flow goes to else statement. Am I missing some codes or logic? Thanks.

I don't want to repeat my code in every if statement that's why I use goto. If you could also suggest other method is also much appreciated.

if (id == -107) goto InsertAdd;
else if (totalAllTk + qty <= 24) goto InsertAdd;
else statusChk = "FULL";

InsertAdd:
    if (itemExists)
    {
         Cart exists = (from item in db.Carts
         where item.ProductId == id && item.SessionId == sessionId && item.SizeId == id select item).FirstOrDefault();
         newQuantity = exists.Quantity + qty;
         exists.Quantity = newQuantity;
         db.SaveChanges();
    }
    else
    {
        Cart newItem = new Cart()
        {
             ProductId = id,
             CreatedBy = userID,
             SessionId = sessionId,
             Quantity = qty,
             SizeId = id,
             Size = tkType,
             CreatedOn = DateTime.Now.ToUniversalTime()
        };
        db.Carts.Add(newItem);
        db.SaveChanges();
        newQuantity = qty;
    }

Solution

  • GOTO statements is just used when you have requirement like when a particular condition occurred we directly jump to a label and the code then starts running from that area.but if the condition do not occur the code will run statement by statement.

    Read how goto works in c# goto (C# Reference)

    But ideally do not depend much on the goto.

    In your code if you want to skip InsertAdd statement in else condition try

     if (id == -107) goto InsertAdd;
     else if (totalAllTk + qty <= 24) goto InsertAdd;
     else statusChk = "FULL";
     return; // to make your function exit in else condition
     InsertAdd:
     if (itemExists)
     {
         Cart exists = (from item in db.Carts
         where item.ProductId == id && item.SessionId == sessionId && item.SizeId == id select item).FirstOrDefault();
         newQuantity = exists.Quantity + qty;
         exists.Quantity = newQuantity;
         db.SaveChanges();
     }
     else
     {
        Cart newItem = new Cart()
        {
             ProductId = id,
             CreatedBy = userID,
             SessionId = sessionId,
             Quantity = qty,
             SizeId = id,
             Size = tkType,
             CreatedOn = DateTime.Now.ToUniversalTime()
        };
        db.Carts.Add(newItem);
        db.SaveChanges();
        newQuantity = qty;
     }
    

    you can also achieve this without using goto

    if (id != -107 || totalAllTk + qty > 24) {   // Running your code here
      if (itemExists)
      {
         Cart exists = (from item in db.Carts
         where item.ProductId == id && item.SessionId == sessionId && item.SizeId == id select item).FirstOrDefault();
         newQuantity = exists.Quantity + qty;
         exists.Quantity = newQuantity;
         db.SaveChanges();
      }
      else
      {
        Cart newItem = new Cart()
        {
             ProductId = id,
             CreatedBy = userID,
             SessionId = sessionId,
             Quantity = qty,
             SizeId = id,
             Size = tkType,
             CreatedOn = DateTime.Now.ToUniversalTime()
        };
        db.Carts.Add(newItem);
        db.SaveChanges();
        newQuantity = qty;
      }
    }
    else {statusChk = "FULL";
    
    }
    

    Hope this Helped