Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

winforms

I am getting this error in condiotional operator.

string remarks="";
AddDgvNew[6, i].Value==null?remarks="":remarks=AddDgvNew[6,i].Value.ToString();
like image 942
Sunny Mark Avatar asked Nov 27 '22 04:11

Sunny Mark


1 Answers

Yes - because you're not doing anything with the result of the conditional expression. You've got a conditional expression which is trying to be a whole statement. In a simpler version:

bool condition = true;
int x = 10;
int y = 5;

// This is invalid
condition ? x : y;

What did you want to do with the result of the conditional expression? If the point was to assign it to a variable, then you need to do that. Currently you have two separate statements: one declares remarks and assigns it a value; the second is just the conditional expression.

If you're trying to do something else, you'll need to clarify what you're looking for.

like image 179
Jon Skeet Avatar answered Dec 15 '22 14:12

Jon Skeet