2017-01-09 39 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