I’m getting an Compilation Error with my CS file. I have a method that creates a JSON object of the user credentials.
public string CreateLoginjson(string strErrorType, bool blIsAuthenticated)
{
StringBuilder sbLoginJson = new StringBuilder();
if (blIsAuthenticated)
{
sbLoginJson.Append("{LoginSuccess:1");
}
else
{
sbLoginJson.Append("{LoginSuccess:0");
}
if (strErrorType != string.Empty)
{
if (strErrorType.TrimEnd(new char[] { ',' }) == "Token" || strErrorType.TrimEnd(new char[] { ',' }) == "BlankToken")
{
sbLoginJson.Append(",txtTestTokenNumber1:\"Error\"");
sbLoginJson.Append(",txtTestTokenNumber2:\"Error\"");
sbLoginJson.Append(",txtTestTokenNumber3:\"Error\"");
sbLoginJson.Append(",txtTestTokenNumber4:\"Error\"");
}
if (strErrorType.TrimEnd(new char[] { ',' }) == "Password" || strErrorType.TrimEnd(new char[] { ',' }) == "BlankPassword")
{
sbLoginJson.Append(",txtPassword:\"Error\"");
}
if (strErrorType.TrimEnd(new char[] { ',' }) == "UserName" || strErrorType.TrimEnd(new char[] { ',' }) == "BlankUserName")
{
{
sbLoginJson.Append(",UserName:\"Error\"");
}
string strLoadErrorControlMessage = LoadErrorControl(strErrorType, string.Empty);
if (strLoadErrorControlMessage != string.Empty)
{
PageTestApplicationLogin objPageTestApplicationLogin = new PageTestApplicationLogin(objClientConfiguration);
sbLoginJson.Append(",ErrorMessage:'" + objPageTestApplicationLogin.GetTestApplicationLoginErrorHtml("", strLoadErrorControlMessage).Replace("'", "\"") + "'");
}
}
sbLoginJson.Append("}");
var LoginJson = sbLoginJson.ToString();
return LoginJson;
}
}
Should I place the return statement elsewhere, cos I feel that’s what’s causing this error?
Thanks in advance
You test
if (strErrorType != string.Empty). But what about if the condition isfalse? What does your method return in this case? Nothing.That’s what cause the compilation error. You must return something, even if it’s
null.Alternatively, you can also raise an exception. For example, you can add :
Going further that simply solving the compilation issue, what is your method supposed to do? You test the emptiness of the strErrorType variable. Should it be disallowed? If yes, throw the exception. If no, return an adequate value (
nullprobably)