2015-11-11 51 views
1

今天我有一個問題,我想問一下。我想弄清楚如何驗證我可以輸入的數組的數量。例如,我想驗證輸入數組的最大數量爲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"); 
      } 

這是可能的還是有沒有更好的方法來做這些?

+1

'產品代碼[]>產品代碼[5]'不編譯 – fubo

+2

我想你的意思'productcode.Length> 5'而不是'productcode []> productcode [5]''。 –

+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; 
    ... 
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 
}