如何验证输入的数组数并验证空白数组

时间:2015-11-11 13:17:26

标签: c# arrays validation

今天我有一个问题,我想问一下。我试图找出如何验证我可以输入的数组的数量。例如,我试图验证输入的最大数组为6。当我尝试时,它有一些错误。

            if (productcode == null || productcode.Length == 0)
            {
                MessageBox.Show("Enter array for productcode");
            }

            if (productcode[] > productcode[5])
            {
                MessageBox.Show("Array for productcode exceeded");
            }

            if (productcode == null || productcode.Length == 0)
            {
                MessageBox.Show("Enter array for product code");

            }
            if (productdesc[] > productdesc[5])
            {
                MessageBox.Show("Array for productdesc exceeded");
            }

            if (quantity == null || quantity.Length == 0)
            {
                MessageBox.Show("Enter array for quantity");
            }
            if(quantity[] > quantity[5])
            {
                MessageBox.Show("Array for quantity exceeded");
            }

            if (batchnumber == null || batchnumber.Length == 0)
            {
                MessageBox.Show("Enter array for batch number");
            }
            if (batchnumber[] > batchnumber[5])
            {
                MessageBox.Show("Array for batch number exceeded");
            }

            if ( dod == null || dod.Length == 0)
            {
                MessageBox.Show("Enter array for dod");
            }
            if (dod[] > dod[5])
            {
                MessageBox.Show("Array for dod exceeded");
            }

            if (unitcost == null || unitcost.Length == 0)
            {
                MessageBox.Show("Enter array for unit cost");
            }
            if (unitcost[] > unitcost[5])
            {
                MessageBox.Show("Array for unitcost exceeded");
            }

这可能还是有更好的办法吗?

2 个答案:

答案 0 :(得分:0)

不是您问题的答案,但我认为您需要产品类别和产品清单:

public class Product 
{
    public string Code {get;set;}
    public string Description {get;set;}
    public int Quantity {get;set;}
    public int BatchNumber {get;set;}
    public string Dod {get;set;}
    public decimal UnitCost {get;set;}

}

清单:

var products = new List<Product>();

验证员:

public void Validate(List<Product> products)
{
    if (products == null || products.Length == 0)
    {
        MessageBox.Show("Too few products");
        return;
    }
    if (products.Length > 5)
    {
        MessageBox.Show("Too many products");
        return;
    }
    // do something useful with the products
}

答案 1 :(得分:0)

不要自己复制!提取方法:

  private static Boolean showArrayErrorText(String name, Array array, int maxLength = 5) {
    if (array == null || array.Length <= 0) {
      MessageBox.Show(String.Format("Enter array for {0}", name));

      return false;
    }
    else if (array.Length > maxLength) {
      MessageBox.Show(String.Format("Array for {0} exceeded", name));

      return false;
    } 

    return true;
  }

...

  showArrayErrorText("productcode", productcode);
  showArrayErrorText("productdesc", productdesc);
  showArrayErrorText("batchnumber", batchnumber);
  ...

如果您想在发现第一个错误后停止验证:

   if (!showArrayErrorText("productcode", productcode))
     return;
   else if (!showArrayErrorText("productdesc", productdesc)) 
     return;
   else if (!showArrayErrorText("batchnumber", batchnumber)) 
     return;
   ...