8

When I read a cell by doing Worksheets.Cells[2,5].value.ToString();

I get a error "System.NullReferenceException: Object reference not set to an instance of an object."

What would be a good way to check for null and then assign the value, without having to have a "if" statement.

4

1 回答 1

14
 string strValue = Worksheets.Cells[2,5].value==null ? string.Empty : Worksheets.Cells[2,5].value.ToString();

或者

object objValue = Worksheets.Cells[2,5].value ?? string.Empty
于 2012-08-15T07:36:25.647 回答