首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在if语句中使用for循环进行验证

如何在if语句中使用for循环进行验证
EN

Stack Overflow用户
提问于 2015-09-18 18:12:16
回答 1查看 74关注 0票数 0

你好,下面的代码是根据网格视图中的产品数量从库存中扣除产品数量的代码。

问题是,我想用类别=1减去产品数量,并进行验证,以检查网格中的所有产品数量是否大于库存中的数量,如果一个产品数量大于存储中的数量,则代码不应执行。

代码语言:javascript
运行
复制
Category Id =gridagree.Rows[index].Cells[7].Text

Product Id = gridagree.Rows[index].Cells[3].Text)

Product Quantity in the grid =gridagree.Rows[index].Cells[5].Text

StoreClass s = new StoreClass();

if(//suggested code)
{

lblmsg.Text="Product quantity is bigger than current balance please change product quantity and try again"// here also can i determine to the user which product has the issue product with id=?
}

else
{
for (int index = 0; index < this.gridagree.Rows.Count; index++)
                    {
                        if (gridagree.Rows[index].Cells[7].Text == "1")
                        {
                            string qun = s.getprodqun(Convert.ToInt32(gridagree.Rows[index].Cells[3].Text));
                            s.subtract(Convert.ToInt32(gridagree.Rows[index].Cells[3].Text), Convert.ToInt32(qun) - Convert.ToInt32(gridagree.Rows[index].Cells[5].Text));
                        }

                    }
}
EN

Stack Overflow用户

回答已采纳

发布于 2015-09-18 18:42:59

我不想评论代码,我只会给你提供我认为你想要的。如何在样本中编码,你需要另一个循环.

代码语言:javascript
运行
复制
int categoryIdIndex = 7;
int productIdIndex = 3;
int quantityRequestedIndex = 5;

List<string> errors = new List<string>()
for (int index = 0; index < this.gridagree.Rows.Count; index++)
{
  Row row = gridagree.Rows[index];
  int categoryId = Convert.ToInt32(row.Cells[categoryIdIndex].Text);
  int productId = Convert.ToInt32(row.Cells[productIdIndex].Text);
  int quantityRequested = Convert.ToInt32(row.Cells[quantityRequestedIndex].Text);
  int availableQuantity = s.getprodqun(productId);

  if(quantityRequested > availableQuantity)
  {
    errors.Add(string.Format("Inventory contains insufficient items for product id {0} ", productId));
  }
}

if(errors.Count == 0)
{
  ... process the orders ...
}
else
{
  ... show the errors
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32658504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档