Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

c#, c#-4.0

Solution

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!)

Problem

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; } ```

Original source

Related problems