I have this foreach section and i am trying to add a line after my "result = string.Format" but i get the following error "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement" can someone tell me what i am doing wrong.
foreach (DataRow record in table.Rows)
{
string key = record["Code"] + "_" + record["Description"];
int row = (int)rownum[key];
string date = formatDate(record["ApptDate"].ToString(), "_");
string result = string.Empty;
if (record["Dosage"].ToString() != string.Empty)
result = string.Format("{0}/{1}", test.SharedLib.Application.RemoveTrailingZeroes(record["Dosage"].ToString()),
test.SharedLib.Application.RemoveTrailingZeroes(record["Units"].ToString()));
if (record["Dosage"].ToString() != string.Empty)
result.StartsWith("/") && result.EndsWith("/") ? result.Replace("/", string.Empty) : result;
else if (record["Units"].ToString() != string.Empty)
result = record["Units"].ToString();
dataTable.Rows[row]["ApptDate" + date] = result;
}
if (record["Dosage"].ToString() != string.Empty)
result.StartsWith("/") && result.EndsWith("/") ? result.Replace("/", string.Empty) : result;
The second line there is not a statement, it is an expression. Not all expressions can be statements in C#, so this is a syntax error.
Presumably you meant to assign the result of this to result
:
if (record["Dosage"].ToString() != string.Empty)
result = (result.StartsWith("/") && result.EndsWith("/")) ? result.Replace("/", string.Empty) : result;
Further, you should consider enclosing the bodies of if
/ else
blocks with braces ({}
). Without braces, the way in which these blocks nest is not intuitive, and it will hamper future maintenance. (For example, can you tell which if
block the else if
block will belong to? Will the next person who inherits this project be able to not only tell the difference, but understand what nesting was intended? Make it explicit!)
You have to do something with the result of your expression (e.g. assign it to result?!)
For more information you should really format and tell us more about your code...SO users are not your personal compiler/debugger ;-)
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