Sunny Mark
Sunny Mark

Reputation: 133

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

I am getting this error in condiotional operator.

string remarks="";
AddDgvNew[6, i].Value==null?remarks="":remarks=AddDgvNew[6,i].Value.ToString();

Upvotes: 4

Views: 42377

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500055

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.

Upvotes: 21

C.Evenhuis
C.Evenhuis

Reputation: 26446

Use

string remarks = AddDgvNew[6, i].Value==null?"":AddDgvNew[6,i].Value.ToString();

Upvotes: 12

Related Questions