I have this set of If statements which work fine but just looks a little ugly. Is there any way I can tart it up instead of using big ole' ugly if statements? The problem is that it uses string.contains which makes it a bit difficult to implement a dictionary, which I tried and failed :(
Here it is:
foreach (var Item in Stuff)
{
var loweredQuoteItem = quoteItem.Name.ToLower();
if (loweredQuoteItem.Contains("one"))
Item.InsurerNameShort = "Item One";
if (loweredQuoteItem.Contains("two"))
Item.InsurerNameShort = "Item Two";
if (loweredQuoteItem.Contains("chaucer"))
Item.InsurerNameShort = "Chaucer";
if (loweredQuoteItem.Contains("three"))
Item.InsurerNameShort = "Item Three";
if (loweredQuoteItem.Contains("four"))
Item.InsurerNameShort = "Item Four";
if (loweredQuoteItem.Contains("five"))
Item.InsurerNameShort = "Item Five";
}
The problem is that it uses string.contains which makes it a bit difficult to implement a dictionary, which I tried and failed :(
Then I guess you implemented it incorrectly. Here's what you should do:
loweredQuoteItem
, loop over your Dictionary.
innerLoopKey
, set Item.InsurerNameShort
to innerLoopValue
(and optionally break)Make sure to construct this Dictionary outside of your foreach (var Item in Stuff)
loop, for better performance.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments