2017-08-07 7 views
-2

項目の量が不十分な場合、if else文が途切れることはありません。それは量が不十分であると表示しますが、依然として継続し、結果としてその量は負になります。else文で終了/中断することはできません

たとえば、製品Aのデータベース数は20です。私は25歳で販売しようとしました。それは "不十分な量"を表示していますが、数量を引いて数量が-5になるようにします。私はそれが "不十分な量"と言うときに停止したい。

これは、あなたがIf文の外でロジックを持っているので、これは単に私のコード

private void btnAddcart_Click(object sender, EventArgs e) 
    { 
     if (!validateProduct()) 
     { 
      return; 
     } 
     else 
     { 
      ShowMyDialogBox(); 
     } 
     if (!alreadyincart()) 
     { 
      return; 
     } 
     else 
     { 

      int str, qty; 
      str = Convert.ToInt32(storeqty.Text); 
      qty = Convert.ToInt32(quantity.Text); 
      temporaryquantity.Text = str.ToString(); 

      if (str < qty || str == 0) 
      { 
       MessageBox.Show("Insufficient Stock", "Error", 
       MessageBoxButtons.OK, MessageBoxIcon.Error); 

       //its supposed to stop here but its still subtracting the 
       // quantity from the product 
      } 
      else 
      { 
       qty = Convert.ToInt32(quantity.Text); 
       unitprice = Convert.ToDouble(dgvPOSproduct.CurrentRow.Cells[6].Value.ToString()); 
       totalprice = qty * unitprice; 
       unittotal.Text = totalprice.ToString("0.00"); 

       addData 
        (
        dgvPOSproduct.CurrentRow.Cells[0].Value.ToString(), //prod id 
        dgvPOSproduct.CurrentRow.Cells[1].Value.ToString(), //brand 
        dgvPOSproduct.CurrentRow.Cells[4].Value.ToString(), //dosage 
        dgvPOSproduct.CurrentRow.Cells[6].Value.ToString(), //qty 
        quantity.Text, 
        unittotal.Text, 
        dgvPOSproduct.CurrentRow.Cells[7].Value.ToString(), 
        dgvPOSproduct.CurrentRow.Cells[8].Value.ToString() 
        ); 
      } 

      int dgvPOSquantity = Convert.ToInt32(dgvPOSproduct.CurrentRow.Cells[5].Value.ToString()); 
      int dgvnewquantity; 
      dgvnewquantity = dgvPOSquantity - qty; 
      dgvPOSproduct.CurrentRow.Cells[5].Value = dgvnewquantity; 

      discountremoveitem(); 

     } 

    } 
+4

あなたが投稿コード化にはループがありません。また、エラーを返した場合は、エラーを説明してください。 –

+0

他の人が指摘しているように、ループを表示する必要があります。明白なことをチェックする:あなたは打ち切りたい点で 'break;'文を試しましたか? – dave

+0

* if else文が壊れていないとはどういう意味ですか?もし条件に応じてifやelseには入りますが、両方ではありません。 –

答えて

2

です。それは、この行を実行する意味

-

if (str < qty || str == 0)

は次に基準に一致してif文を飛び出し、減算を行い、次の行

int dgvPOSquantity

を打ちます。

控除コードをelse文に移動するか、ロジックフローを変更します。例えば

if (str < qty || str == 0) 
     { 
      MessageBox.Show("Insufficient Stock", "Error", 
      MessageBoxButtons.OK, MessageBoxIcon.Error); 
//This statement is getting hit and exits the IF Statement 
     } 
     else 
     { 
      qty = Convert.ToInt32(quantity.Text); 
      unitprice = Convert.ToDouble(dgvPOSproduct.CurrentRow.Cells[6].Value.ToString()); 
      totalprice = qty * unitprice; 
      unittotal.Text = totalprice.ToString("0.00"); 

      addData 
       (
       dgvPOSproduct.CurrentRow.Cells[0].Value.ToString(), //prod id 
       dgvPOSproduct.CurrentRow.Cells[1].Value.ToString(), //brand 
       dgvPOSproduct.CurrentRow.Cells[4].Value.ToString(), //dosage 
       dgvPOSproduct.CurrentRow.Cells[6].Value.ToString(), //qty 
       quantity.Text, 
       unittotal.Text, 
       dgvPOSproduct.CurrentRow.Cells[7].Value.ToString(), 
       dgvPOSproduct.CurrentRow.Cells[8].Value.ToString() 
       ); 
     } 

//But then carries on from here, which does the subtraction. 
//You need to either move this code snippet into the else statement, or change the flow. 

     int dgvPOSquantity = Convert.ToInt32(dgvPOSproduct.CurrentRow.Cells[5].Value.ToString()); 
     int dgvnewquantity; 
     dgvnewquantity = dgvPOSquantity - qty; 
     dgvPOSproduct.CurrentRow.Cells[5].Value = dgvnewquantity; 

     discountremoveitem(); 

    } 
関連する問題