2

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;
    }
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
LeViNcE
  • 304
  • 1
  • 6
  • 15
  • 1
    Yes, your code is correct. And it will work that way. After executing else statement, it will execute next line as normal flow. If you don't want to execute it then you have to use another `label` and write `goto NewLabel` after else statement. – Karan Oct 10 '18 at 04:09
  • 1
    Please refer to https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/goto – Miller Cy Chan Oct 10 '18 at 04:13
  • 6
    Just don’t use goto ever – maccettura Oct 10 '18 at 04:13
  • Are `someCondition` and `someCondition` actually the same? Also `someStatement`? If not, consider using different placeholders to show that they are different. – moreON Oct 10 '18 at 04:16
  • @moreON I've updated my question. Actual codes are now indicated – LeViNcE Oct 10 '18 at 04:23
  • 1
    Make it local function and call it instead of `goto`. – user4003407 Oct 10 '18 at 04:27
  • Just use `statusChk = (id == -107) || (totalAllTk + qty <= 24) ? statusChk : "FULL";` instead of your if statements -HTH ;). – shA.t Oct 10 '18 at 04:32

2 Answers2

4

goto is unnecessary here.

First of all, code is executed line by line, so if statement is executed first, then it goes to next lines, which is your InsertAdd "section". But this label doesn't prevent the code underneath from executing. So you need to structure your code differently (I'll show how below).

Moreover, why having else if if you want to take exact same action as in if? You should simplify it to:

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

But still, InsertAdd gets executed even if if fails. To prevent it, you could place all InsertAdd code inside the if (no you won't have duplicate code).

Even better solution is to declare a method:

public void InsertAdd(){ // InsertAdd code here }

And use it like this:

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

Even if you want to stick to your initial if statement, it's easy to use the method:

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

This way, code inside InsertAdd() won't be executed if code goes to else part :)

Generally, goto is discouraged. See this article.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
3

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

Amit chauhan
  • 540
  • 9
  • 22