? and : for conditional expressions in v2

M

Mark Rae

Hi,

In v1.x, the following code worked perfectly:

mobjSqlParameter.Value = strPayerBusinessName == null ? strPayerBusinessName
: (object)DBNull.Value;

However, I can't get it to work in v2. I know that the value of
strPayerBusinessName is null because if I write

strPayerBusinessName == null in the Immediate Window, it returns "true".

However, the expression does not seem to evaluate the right-hand side, no
matter what I write. E.g., the following code

mobjSqlParameter.Value = strPayerBusinessName == null ? strPayerBusinessName
: "Hello";

does not populate mobjSqlParameter.Value with the string "Hello" when
strPayerBusinessName is null.

Has this syntax been deprecated in v2?

I realise, of course, that I can write:

if(strPayerBusinessName == null)
{
mobjSqlParameter.Value = DBNull.Value;
}
else
{
mobjSqlParameter.Value = strPayerBusinessName;
}

but I'm curious as to why the ? : syntax doesn't seem to work any more.

Any assistance gratefully received.

Mark
 
C

Christoph Nahr

?: works the same as ever, but did you notice that you're using the
wrong comparison? You're returning Name when Name is null, and I
don't think that's what you intend to do! Use != instead of == or
else reverse the : branches.
 
J

Jon Skeet [C# MVP]

Mark Rae said:
In v1.x, the following code worked perfectly:

mobjSqlParameter.Value = strPayerBusinessName == null ? strPayerBusinessName
: (object)DBNull.Value;

However, I can't get it to work in v2. I know that the value of
strPayerBusinessName is null because if I write

strPayerBusinessName == null in the Immediate Window, it returns "true".

However, the expression does not seem to evaluate the right-hand side, no
matter what I write. E.g., the following code

mobjSqlParameter.Value = strPayerBusinessName == null ? strPayerBusinessName
: "Hello";

does not populate mobjSqlParameter.Value with the string "Hello" when
strPayerBusinessName is null.

Has this syntax been deprecated in v2?

No. I suspect something else is going on.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

You might be interested in the null coalescing operator, however, which
would let you do:

mobjSqlParameter.Value = strPayerBusinessName ?? "Hello";

See http://www.pobox.com/~skeet/csharp/csharp2/nullable.html for more
details.
 
M

Michael S

Mark Rae said:
Perfect! Thanks a lot, Jon, as always!


Incredibly useful!

Yes, it's a marvel.

I do alot of web applications and while a database might find null a good
thing, we want to generate text and really hates null strings. Whatever
happens, a website sporting a brand or a trademark must never crash. And
boy, do we hate the 'Object reference not set to an instance of an object'
error..

The coalescing operator is a dream come true for us:
string someinput = Request["someinput"] ?? "";

Anyways, back to your problem, I think you shoud use () more often. This is
how I'd do it.
string s = ((a == b) ? "a" : "b");

Now I have a question for the masters of english wanting to help a swede
like me:
What da foo does 'coalescing' means and how on earth do you pronounce that
darn word?

Happy Coalescing
- Michael S
 
M

Mark Rae

This is how I'd do it.
string s = ((a == b) ? "a" : "b");

How would that improve things...?
What da foo does 'coalescing' means and how on earth do you pronounce that
darn word?

"Coalescing", literally, means mixing together different elements / fusing
or causing to grow together. It's yet another example of a perfectly good
English word being hijacked by our friends in Seattle, cf "boxing". It's
pronounced co-a-LESS-ing

Still, nothing yet has come close to the abomination of using "leverage" as
a verb...
 
J

Jon Skeet [C# MVP]

Now I have a question for the masters of english wanting to help a swede
like me:
What da foo does 'coalescing' means and how on earth do you pronounce that
darn word?

To "coalesce" things is to mix them, blend them together. It's
pronounced:

co (like the co of coke)
a (like the "er" at the end of bigger or better)
lesce (like "less" as in the opposite of "more")
 
O

Otis Mukinfus

How would that improve things...?


"Coalescing", literally, means mixing together different elements / fusing
or causing to grow together. It's yet another example of a perfectly good
English word being hijacked by our friends in Seattle, cf "boxing". It's
pronounced co-a-LESS-ing

Still, nothing yet has come close to the abomination of using "leverage" as
a verb...
I'm glad you definitized how you feel about that Mark ;o)

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
M

Mark Rae

I'm glad you definitized how you feel about that Mark ;o)

ROTFLMAO! Nice one! If anyone ever burglarizes me, I'll certainly make sure
they're hospitalized... :)

Now then, let me just find a sentence in which I can use the phrase "off
of"...
 
B

Bruce Wood

I wouldn't say "er" as in "better". It's actually the schwa: an
unstressed vowel sound, such as "a" in "above" or "o" in "melon", often
transcribed as "uh", so:

co-uh-LESS
 
J

Jon Skeet [C# MVP]

Bruce said:
I wouldn't say "er" as in "better". It's actually the schwa: an
unstressed vowel sound, such as "a" in "above" or "o" in "melon", often
transcribed as "uh", so:

co-uh-LESS

"uh" is good, yes. As it happens, I pronounce the "er" of better in the
same way as the "a" of above, but slightly differently to the "o" of
"melon"... (I'd be hard pressed to describe the exact difference,
admittedly.)

Jon
 
M

Michael S

Well stop it now =)

While asking for a joke, I got some jokes. Now it's getting polical and
even a troll like me knows this is a no-no in this forum.

Happy Coding
- Michael S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top