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

前端 未结 3 647
既然无缘
既然无缘 2020-12-19 10:25

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, aw

相关标签:
3条回答
  • 2020-12-19 10:58
    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!)

    0 讨论(0)
  • 2020-12-19 11:10

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

    0 讨论(0)
  • 2020-12-19 11:13
    result.StartsWith("/") && result.EndsWith("/") ? result.Replace("/", string.Empty) : result;
    

    This line does nothing with the result of the ternary operator. I assume you'd want to assign it to result

    0 讨论(0)
提交回复
热议问题