有没有更好的方法来做到这一点(可能是使用方法)?

Is there a better way to do this (may be using methods)?

C#
这里的 orders 是一个 JSON 对象。我有 3 个可能是 int 的字符串,我正在验证它(使用扩展方法),但是我似乎在为每个变量重复相同的代码。我正在寻找一种优化方法,因为它似乎有些冗长。 我也可以在这里使用日期或双重检查吗?

            int itemNumber;
            int customer;
            DateTime date = DateTime.Now;
            string dateFormatted = date.ToString("dd-MM-yyyy");
            int itemQuantity;
            double itemCost;

            bool result;
            int orderNum = int.Parse(orders.OrderNumber);//convert string to int

            int validInt;
            double validDbl;

            //loop through each of the orders
            foreach (var order in orders.OrderDetails)
            {
                string item = order.ItemNumber;
                string cusNumber = order.CustomerNumber;
                string orderDate = order.OrderDate;
                string quantity = order.Quantity;
                string cost = order.Cost;

                //string field;

                result = item.isNumber();

                // Validate item number
                validInt = validate(item);


                //}
                int counter = 0;
                if (validInt != 0)
                {
                    itemNumber = validInt;
                    counter++;
                }
                else
                {
                    errMessage = $"{item} is not a valid integer value.";
                    errors.Add(errMessage);

                }

                // Validate customer number
                validInt = validate(cusNumber);
              
                if (validInt != 0)
                {
                    customer = validInt;
                    counter++;
                }
                else
                {
                    errMessage = $"{cusNumber} is not a valid integer value.";
                    errors.Add(errMessage);
                }

                

                validInt = validate(quantity);
                if (validInt != 0)
                {
                    itemQuantity = validInt;
                    counter++;
                }
                else
                {
                    errMessage = $"{quantity} is not a valid integer value.";
                    errors.Add(errMessage);
                }

                validDbl = isDouble(cost);
                if (validDbl != 0)
                {
                    itemCost= validDbl;
                    counter++;
                }
                else
                {
                    errMessage = $"{quantity} is not a valid integer value.";
                    errors.Add(errMessage);
                }



            }

如果计数器 = JSON 对象(数组)中的项目总数,我将使用计数器和 errMessage 显示正确的订单。

更智能的验证

 static bool validate(string input, out int field, ref int counter, List<string> errors){
       bool ok = Int32.TryParse(input, out field);
       if(ok){
            counter++;
       }else{
           errors.Add($"{input} is not a valid integer value.");
       }
       return ok; // just in case we need it
   }

现在

    validate(item, out itemNumber, ref counter, errors);
    validate(cusNumber, out customer, ref counter, errors);
    validate(quantity, out itemQuantity, ref counter, errors);
    .....
       

或者你可以验证一个成员函数并将计数器和错误作为成员变量,在这种情况下它们不需要作为参数传递