I have this really long if else code
if (errorNumbers.Length == 6)
{
if (errorNumbers.Substring(0,4).Equals("1101") || errorNumbers.Substring(0,4).Equals("2121"))
{
retStr = "AutoRepickAfterPickError";
}
else if (errorNumbers.Substring(0, 4).Equals("1301") || errorNumbers.Substring(0, 4).Equals("2321"))
{
retStr = "AutoRepickAfterLAlignError";
}
else if (errorNumbers.Substring(0, 4).Equals("1401") || errorNumbers.Substring(0, 4).Equals("2221"))
{
retStr = "AutoRepickAfterCAlignError";
}
else if (errorNumbers.Substring(0, 4).Equals("1501") || errorNumbers.Substring(0, 4).Equals("2041"))
{
retStr = "AutoRepicksAfterManualRecovery";
}
etc.....
How I can rewrite it to something more nice . Trying to learn here something new , and I am in .net 2.0. Thanks for help.
I´d map the error codes in a dictionary like this:
string errorCode = errorNumbers.Substring(0, 4);
Dictionary<string, string> map = new Dictionary<string,string>();
map.Add("1501", "AutoRepicksAfterManualRecovery");
map.Add("2041", "AutoRepicksAfterManualRecovery");
map.Add("1401", "AutoRepickAfterCAlignError");
map.Add("2221", "AutoRepickAfterCAlignError");
map.Add("1301", "AutoRepickAfterPickError");
map.Add("2121", "AutoRepickAfterPickError");
// etc
if(!map.ContainsKey(errorCode))
throw new Exception("Invalid error code");
return map[errorCode];
That should be pretty straightforward
if (errorNumbers.Length == 6)
{
string errNo = errorNumbers.Substring(0, 4);
switch (errNo)
{
case "1101":
case "2121":
retStr = "AutoRepickAfterPickError";
break;
case "1301":
case "2321":
retStr = "AutoRepickAfterLAlignError";
break;
case "1401":
case "2221":
retStr = "AutoRepickAfterCAlignError";
break;
case "1501":
case "2041":
retStr = "AutoRepicksAfterManualRecovery";
break;
}
}
Also note that unlike in Java, in C# you can compare strings with ==
.
"123" == "456"
does the same thing as "123".Equals("456")
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With