Error message that I don't understand

I am getting "Value assigned is not used in any execution path". Why when
it is used as the return value?

string errorMessage = string.Empty;
if (AccPacSession.Errors == null)
errorMessage = string.Format("Exception Text: \r\nInner Exception Text: ", Ex.Message, Ex.InnerException.Message); else { errorMessage = "AccPac Errors:\r\n"; for (int i = 0; i <= AccPacSession.Errors.Count - 1; i++) errorMessage += string.Format("\r\n",
AccPacSession.Errors+.Message);
AccPacSession.Errors.Clear();
errorMessage += string.Format("\r\nException Text:
\r\nInner Exception Text: ", Ex.Message == null ? "None" : Ex.Message,
Ex.InnerException == null ? "None" : Ex.InnerException.Message);
errorMessage += "\r\n";
}

return errorMessage;

1 comment

Hello Rob,

Not variable itself, but value assigned in initialization expression (that
is, string.Empty) is overwritten on any execution path. The very first assignment
in every "if" branch throws away previous value.

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


RT> I am getting "Value assigned is not used in any execution path".
RT> Why when it is used as the return value?
RT>
RT> string errorMessage = string.Empty;
RT> if (AccPacSession.Errors == null)
RT> errorMessage = string.Format("Exception Text:
RT> \r\nInner RT> Exception Text: ", Ex.Message, Ex.InnerException.Message); RT> else RT> { RT> errorMessage = "AccPac Errors:\r\n"; RT> for (int i = 0; i <= AccPacSession.Errors.Count - 1; RT> i++) RT> errorMessage += string.Format("\r\n",
RT> AccPacSession.Errors+.Message);
RT> AccPacSession.Errors.Clear();
RT> errorMessage += string.Format("\r\nException Text:
RT> \r\nInner Exception Text: ", Ex.Message == null ? "None" :
RT> Ex.Message,
RT> Ex.InnerException == null ? "None" : Ex.InnerException.Message);
RT> errorMessage += "\r\n";
RT> }
RT> return errorMessage;
RT>


0

Please sign in to leave a comment.