2017-01-09 35 views
-1

下面的代碼我想要做什麼,除了實在是太硬編碼的事實,會顯示建築材料與值0。現在,它可能會顯示如何過濾字符串不IF語句C#

木:100% ,砌體:0%,混凝土:0%,鋼:0%,輕金屬:0%,移動房屋:0%,其他:0%,未知:0%

我希望在這種情況下顯示

木:100%

我是一個極端的業餘,我知道該怎麼做THI的唯一途徑s有10000個IF語句,但必須有更優雅的方式。函數convertFIRE是映射到這些主要結構「桶」

for (int row = firstrow + 1; row <= sheet5.LastRowNum; row++) 
{ 
    convertFIRE(sheet5.GetRow(row).GetCell(1).ToString()); 

    if (constructioncode == "Wood") 
    { 
     wood = wood + sheet5.GetRow(row).GetCell(10).NumericCellValue; 
    } 
    else if (constructioncode == "Masonry") 
    { 
     masonry = masonry + sheet5.GetRow(row).GetCell(10).NumericCellValue; 
    } 
    else if (constructioncode == "Concrete") 
    { 
     concrete = concrete + sheet5.GetRow(row).GetCell(10).NumericCellValue; 
    } 
    else if (constructioncode == "Steel") 
    { 
     steel = steel + sheet5.GetRow(row).GetCell(10).NumericCellValue; 
    } 
    else if (constructioncode == "Light Metal") 
    { 
     lghtmetal = lghtmetal + sheet5.GetRow(row).GetCell(10).NumericCellValue; 
    } 
    else if (constructioncode == "Mobile Home") 
    { 
     mobilehome = mobilehome + sheet5.GetRow(row).GetCell(10).NumericCellValue; 
    } 
    else if (constructioncode == "Other") 
    { 
     other = other + sheet5.GetRow(row).GetCell(10).NumericCellValue; 
    } 
    else if (constructioncode == "Unknown") 
    { 
     unknowncode = unknowncode + sheet5.GetRow(row).GetCell(10).NumericCellValue; 
    } 

} 
constructiontext = "Wood: " + String.Format("{0:P1}", wood) + ", " + "Masonry: " + String.Format("{0:P1}", masonry) + ", " + "Concrete: " + String.Format("{0:P1}", concrete) + ", " 
    + "Steel: " + String.Format("{0:P1}", steel) + ", " + "Light Metal: " + String.Format("{0:P1}", lghtmetal) + ", " + "Mobile Home: " + String.Format("{0:P1}", mobilehome) + ", " + "Other: " + String.Format("{0:P1}.", other) + ", " + "Unknown: " + String.Format("{0:P1}", unknowncode); 
+0

我想你應該閱讀http://www.codinghelmet.com/?path=howto/poor-mans-polymorphism-or-whats-so-wrong-about-if-那麼 - 否則 –

+0

應該可能去Review.StackExchange.com,不是嗎? – HimBromBeere

+0

@HimBromBeere我不太確定現在「重構此代碼」是否會成爲[CodeReview.SE]的主題,但您是對的 - 在過去,完全相同的問題是確定的 - http://codereview.stackexchange .com/questions/46530/how-to-refactor-multiple-if-statements –

回答

5

因此,您有幾個變量來跟蹤基於關鍵字符串的數字。而不是多個變量,你可以使用一個Dictionary<string, decimal>

Dictionary<string, decimal> percentages = new Dictionary<string, decimal>(); 
for (int row = firstrow + 1; row <= sheet5.LastRowNum; row++) 
{ 
    convertFIRE(sheet5.GetRow(row).GetCell(1).ToString()); 
    string key = constructioncode; 
    decimal value = sheet5.GetRow(row).GetCell(10).NumericCellValue; 
    if(percentages.ContainsKey(key); // does the key already exist? 
     percentages[key] += value; // add to the value 
    else        // else 
     percentages[key] = value;  // add the key and value to the dictionary 

然後,您可以通過鍵值對,只輸出鍵和值循環,如果該值存在,和/或大於0

+0

這與下面的結合很好,我今天學到了很多東西,還有很多其他我可以實現類似的地方! – James

0

只需製作一個Dictionary<string, int>,其中string -Key將是您的"Steel"等等。 然後你可以做這樣的事情:

if (dic.ContainsKey(constructioncode)) { 
    dic[constructioncode] += sheet5.GetRow(row).GetCell(10).NumericCellValue 
} else { 
    //Maybe add as new key??? 
    dic.Add(constructioncode, sheet5.GetRow(row).GetCell(10).NumericCellValue); 
} 
0

由於沒有其他的答案中描述瞭如何把一個字典到你想要的字符串,這裏的一部分:

constructiontext = string.Join(", ", dictionary.Where(kv => kv.Value != 0).Select(kv => kv.Key + ": " + kv.Value + "%").ToArray()); 

而且,你的代碼是如果您使用camelCase(在以變量名開始新子字時使用大寫字母),則可讀性更高。這是編寫C#代碼時普遍接受的標準,這意味着之後需要維護代碼的任何人都可以立即開始這樣做。因此,而不是constructioncode,請寫constructionCode。而不是constructiontext,請寫constructionText。而不是mobilehome,請寫mobileHome

+0

感謝您的提示! – James