aeon
aeon

Reputation: 11

Weird behavior of conditional operator?

string sortedcolumn =
( dataGridView1.SortedColumn != null
? dataGridView1.SortedColumn.Name
: "username"
);

In above statment when dataGridView1.SortedColumn==null I get exception of dataGridView1.SortedColumn is null instead of getting value as "username" to sortedcolumn var.

Any idea?

Upvotes: 0

Views: 147

Answers (1)

Timwi
Timwi

Reputation: 66573

Possible causes:

  • dataGridView1 is a field or variable containing the value null.

  • dataGridView1 is a property getter that returns null.

  • SortedColumn is a weird property getter that returns something non-null the first time but null afterwards. (Very unlikely because then merely looking at it in the debugger would change the result.)

  • dataGridView1, SortedColumn, or Name is a property getter that throws the exception you are seeing.

  • dataGridView1.SortedColumn is of a type that overloads operator!=, which throws the exception you are seeing.

  • dataGridView1.SortedColumn.Name is of a type that implements implicit operator string, which throws the exception you are seeing.

Upvotes: 2

Related Questions