2013-01-21 45 views
-1

所以atm即時進口excel文檔到我的項目。然後,如果電子表格中的任何列都爲空,我將使用if語句來顯示相應的錯誤消息。我的代碼正在工作,但它看起來並不整齊。有無論如何重構所有的if語句到循環顯示錯誤消息,可能是另一種方法,檢查所有? checkIfColumnIsEmpty是如果列是空重構if statemets

//if either column 0 and 1 are empty && column 2,3,4 and 5 are not 
    if (!checkIfColumnisEmpty(r.ItemArray[0]) || !checkIfColumnisEmpty(r.ItemArray[1]) 
     && checkIfColumnisEmpty(r.ItemArray[2]) && checkIfColumnisEmpty(r.ItemArray[3]) 
     && checkIfColumnisEmpty(r.ItemArray[4]) && checkIfColumnisEmpty(r.ItemArray[5])) 
    { 
     if (checkIfColumnisEmpty(r.ItemArray[0]) && !checkIfColumnisEmpty(r.ItemArray[1])) 
     { 
      throw new ImportBOQException("Error importing document: First column is empty"); 
     } 
     else if (!checkIfColumnisEmpty(r.ItemArray[0]) && checkIfColumnisEmpty(r.ItemArray[1])) 
     { 
      throw new ImportBOQException("Error importing document: Second column is empty"); 
     } 

     else if (!checkIfColumnisEmpty(r.ItemArray[0]) && !checkIfColumnisEmpty(r.ItemArray[1])) 
     { 
      //all columns are valid so... 
      Column0inSpreadsheet = r.ItemArray[0] as string; 
      Column1inSpreadsheet = r.ItemArray[1] as string; 

      //Other code which performs other operations, once the level as reached this far 
     } 
    }         

    //if column 0 and 1 are NOT empty && Either column 2,3,4 or 5 is empty 
    else if (checkIfColumnisEmpty(r.ItemArray[0]) && checkIfColumnisEmpty(r.ItemArray[1]) 
      || !checkIfColumnisEmpty(r.ItemArray[2]) || !checkIfColumnisEmpty(r.ItemArray[3]) 
      || !checkIfColumnisEmpty(r.ItemArray[4]) || !checkIfColumnisEmpty(r.ItemArray[5])) 
    { 
     if (checkIfColumnisEmpty(r.ItemArray[2])) 
     { 
      throw new ImportBOQException("Error importing document: Third column is empty"); 
     } 
     else if (checkIfColumnisEmpty(r.ItemArray[3])) 
     { 
      throw new ImportBOQException("Error importing document: Fourth column is empty"); 
     } 
     else if (checkIfColumnisEmpty(r.ItemArray[4])) 
     { 
      throw new ImportBOQException("Error importing document: Fifth column is empty"); 
     } 
     else if (checkIfColumnisEmpty(r.ItemArray[5])) 
     { 
      throw new ImportBOQException("Error importing document: Sixth column is empty"); 
     } 
     else 
     //all columns are valid so... 
     { Column2inSpreadsheet = (r.ItemArray[2]) as string; 
      Column3inSpreadsheet = (r.ItemArray[3]) as string; 
      Column4inSpreadsheet = (r.ItemArray[4]) as string; 
      Column5inSpreadsheet = (r.ItemArray[5]) as string; 

      //Other code which performs other operations, once the level as reached this far 
     } 
    } 
    else 
    //other errors ot related to empty colums 
    { 
     throw new Exception("Error Uploading"); 
    } 
} 
+3

我認爲這個問題屬於到http://codereview.stackexchange。 com/ –

+0

好吧,貼在那裏謝謝 – John

回答

1

如果你需要的是檢查的一列是否是「空」,並得到它的指數,您可以使用此:

var firstEmptyColumn = r.ItemArray 
    .Select((f, i) => new { Field=f, Index=i }) 
    .FirstOrDefault(x => r.IsNull(x.Index) || x.Field.ToString().Trim().Length == 0); 
if (firstEmptyColumn != null) 
{ 
    string errorMsg = string.Format("Error importing document: {0} column is empty", 
     firstEmptyColumn.Index); 
    throw new ImportBOQException(errorMsg); 
} 
+0

好吧,謝謝蒂姆,虐待這一切 – John

1

首先,轉換R鍵類命名的屬性,而不是使用數組索引並從源初始化集合返回true一個布爾值方法。 第二,創建帶簽名的幫助方法 bool VerifyColumn(字符串值,字符串propertyName)。這是一個基礎重構,也許你可以將你的邏輯從「Holy s ***」改爲「這是一團糟,但我們可以改變它!」

+0

好的,謝謝你,ItemArray是從電子表格導入列如r.ItemArray [0]是第一列 – John