开发者

using the ?? operator and dealing with a null value

开发者 https://www.devze.com 2023-03-21 02:09 出处:网络
I am returning a scalar value from a SQL Server 2008 database: string reason = cmd.ExecuteScalar().ToString() ?? : \"\";

I am returning a scalar value from a SQL Server 2008 database:

string reason = cmd.ExecuteScalar().ToString() ?? : "";

I want to make sure that if null is returned, that reason = "" and not null.

i am getting an error on this line:

Error 3 Invalid expression term ':'

How can this be fixed?

EDIT:

thank you for the changes on the colon, now i am getting this exception on the same line:

string reason = cmd.ExecuteScalar().ToString() ?? "";
System.NullReferenceException occurred
  Message="Object reference not set to an instance of an obj开发者_Go百科ect."


Try this:

string reason = cmd.ExecuteScalar().ToString() ?? "";

BUT: this will still fail, since if .ExecuteScalar() returns a NULL, you're already causing a Null Reference Exception by calling .ToString() on that NULL value......

So I guess the ?? operator really doesn't help you here... do the "usual" dance:

object result = cmd.ExecuteScalar();

if(result != null)
{  
   reason = result.ToString();
}
else
{
   reason = "(NULL value returned)";
}


First, you shouldn't have the : when using the ?? operator.

Second, to do what you are trying to do here without getting an error, you need to do it differently:

object objReason = cmd.ExecuteScalar();
string reason = objReason == null ? "" : objReason.ToString();

This will check whether or not your returned value is null and if it is, the second line will set reason to a blank string, otherwise it will use your returned value.


Since ExecuteScalar() returns object that might be null you should not call .ToString() since that may throw and exception.

string reason = Convert.ToString(cmd.ExecuteScalar());

This works because Convert.ToString() will convert null to string.Empty


or if you must use ?? because you really like it:

(cmd.ExecuteScalar() ?? (object)"").ToString();


Just get rid of the colon.

string reason = cmd.ExecuteScalar().ToString() ?? "";

For reference, check the MSDN page.


When using the null-coalescing operator, you don't need the colon:

string reason = cmd.ExecuteScalar().ToString() ?? "";

As others have pointed out though, ToString() would cause a NullReferenceExcpetion to be thrown anyway...so you don't gain anything here. You'd be much better off splitting this into multiple lines:

var result = cmd.ExecuteScalar();
string reason = result == null ? "" : result.ToString();


You're confusing the ? conditional operator, the syntax for which looks like this:

String x = condition ? valueIfConditionIsTrue : valueIfConditionIsFalse;

with the ?? null-coalesce operator whose syntax is as follows:

String x = possiblyNull ?? valueIfPossiblyNullIsNull;

So, apart from all that... this is the part you really want:

String reason = (cmd.ExecuteScalar() ?? "").ToString();

This takes care of your exception where ToString() was causing a null-reference exception.


Just use

string reason = cmd.ExecuteScalar() ??  "";
0

精彩评论

暂无评论...
验证码 换一张
取 消