syntax for ? :

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Below is code that populates a single SQL Server parameter value. The
parameter is a varchar, but could be a null value. Why doesn't the ? :
syntax in the second piece of code work?? Isn't it identical to the first
option? The error claims that there is no way to convert DBNull.Value to a
string. Does a ? : have to have a consistent format in the THEN and ELSE?
Thanks!
-Mark

*** WORKS ***

if (strCategory.Equals(string.Empty))
{
sqlComm.Parameters["@task_category"].Value = DBNull.Value;
}
else
{
sqlComm.Parameters["@task_category"].Value = strCategory;
}

*** DOESN'T WORK ***

sqlComm.Parameters["@task_category"].Value =
(strCategory.Equals(string.Empty)) ? DBNull.Value : strCategory;
 
Mark said:
Below is code that populates a single SQL Server parameter value. The
parameter is a varchar, but could be a null value. Why doesn't the ? :
syntax in the second piece of code work?? Isn't it identical to the first
option? The error claims that there is no way to convert DBNull.Value to a
string. Does a ? : have to have a consistent format in the THEN and ELSE?
Thanks!

The THEN and ELSE parts of the ternary either must have the same type, or
there must be an implicit conversion between the type of the THEN part
and the type of the ELSE part.

DBNull.Value is of type DBNull, and strCategory is a string, so the
two halfs have different types with no implicit conversion.

Your first version works because both string and DBNull can be assigned
to object.

Cheers,

Michi.
 
Mark,
*** DOESN'T WORK ***

sqlComm.Parameters["@task_category"].Value =
(strCategory.Equals(string.Empty)) ? DBNull.Value : strCategory;

This should work

sqlComm.Parameters["@task_category"].Value =
(strCategory.Equals(string.Empty)) ? (object)DBNull.Value :
(object)strCategory;



Mattias
 
Yes, ?: operator requires both expressions to be of the same type. I
think you can upcast both expressions to System.Object:


sqlComm.Parameters["@task_category"].Value =
(strCategory.Equals(string.Empty)) ? (object) DBNull.Value : (object)
strCategory;

However, I prefer the if-then-else syntax. It's much more readable.

Avner
 
You see, thats what you get for trying to be a Mr.Fancy.Pants.

Seen it before (and just 10 minuites ago) this tw.at comes in blaming me on
a bug in a UI control, and Im uhh works here (as one usually does :D) and
then I found the bug, he has this bit of code thats so trying to be the
UNIFIED THEORY OF EVERYTHING in 3 lines using some stupid try to be all code
line.

Laughable, seen it before, trying to show off but fails :D



Neri said:
Yes, ?: operator requires both expressions to be of the same type. I
think you can upcast both expressions to System.Object:


sqlComm.Parameters["@task_category"].Value =
(strCategory.Equals(string.Empty)) ? (object) DBNull.Value : (object)
strCategory;

However, I prefer the if-then-else syntax. It's much more readable.

Avner


"Mark" <[email protected]> wrote in message
Below is code that populates a single SQL Server parameter value. The
parameter is a varchar, but could be a null value. Why doesn't the ? :
syntax in the second piece of code work?? Isn't it identical to the first
option? The error claims that there is no way to convert DBNull.Value to a
string. Does a ? : have to have a consistent format in the THEN and ELSE?
Thanks!
-Mark

*** WORKS ***

if (strCategory.Equals(string.Empty))
{
sqlComm.Parameters["@task_category"].Value = DBNull.Value;
}
else
{
sqlComm.Parameters["@task_category"].Value = strCategory;
}

*** DOESN'T WORK ***

sqlComm.Parameters["@task_category"].Value =
(strCategory.Equals(string.Empty)) ? DBNull.Value : strCategory;
 
Back
Top