471,337 Members | 977 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

? and : for conditional expressions in v2

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
Jan 28 '06 #1
14 1493
?: 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.
--
http://www.kynosarges.de
Jan 28 '06 #2
Mark Rae <ma**@markN-O-S-P-A-M.co.uk> wrote:
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/c.../nullable.html for more
details.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 28 '06 #3
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
You might be interested in the null coalescing operator, however, which
would let you do:

mobjSqlParameter.Value = strPayerBusinessName ?? "Hello";
Perfect! Thanks a lot, Jon, as always!
See http://www.pobox.com/~skeet/csharp/c.../nullable.html for more
details.


Incredibly useful!
Jan 28 '06 #4
"Mark Rae" <ma**@markN-O-S-P-A-M.co.uk> wrote in message
news:uX**************@TK2MSFTNGP14.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
You might be interested in the null coalescing operator, however, which
would let you do:

mobjSqlParameter.Value = strPayerBusinessName ?? "Hello";


Perfect! Thanks a lot, Jon, as always!
See http://www.pobox.com/~skeet/csharp/c.../nullable.html for more
details.


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
Jan 28 '06 #5
"Michael S" <no@mail.com> wrote in message
news:e8*************@TK2MSFTNGP15.phx.gbl...
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...
Jan 28 '06 #6
Michael S <no@mail.com> wrote:

<snip>
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")

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 28 '06 #7
On Sat, 28 Jan 2006 22:11:41 -0000, "Mark Rae"
<ma**@markN-O-S-P-A-M.co.uk> wrote:
"Michael S" <no@mail.com> wrote in message
news:e8*************@TK2MSFTNGP15.phx.gbl...
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...

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

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
Jan 29 '06 #8
"Otis Mukinfus" <ph***@emailaddress.com> wrote in message
news:37********************************@4ax.com...
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)


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"...
Jan 29 '06 #9

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Michael S <no@mail.com> wrote:
... 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")


Co-ER-less? Hey, no teaching people the redneck version! :)
http://www.m-w.com/cgi-bin/audio.pl?coales01.wav


Jan 30 '06 #10
Mike <vi********@yahoo.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Michael S <no@mail.com> wrote:
... 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")


Co-ER-less? Hey, no teaching people the redneck version! :)
http://www.m-w.com/cgi-bin/audio.pl?coales01.wav


That's got the "er" though. There's no emphasis or length on it - but
the main thing is that it's not "coal"-"less" (as in a lack of coal) :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 30 '06 #11
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

Jan 30 '06 #12
Sounds like a extract from a campaign speech by Dubya ;-p
Jan 30 '06 #13
Bruce Wood wrote:
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

Jan 30 '06 #14
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
Jan 30 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Ney André de Mello Zunino | last post: by
62 posts views Thread by Reinhold Birkenfeld | last post: by
92 posts views Thread by Raghavendra R A V, CSS India | last post: by
6 posts views Thread by Chris Dunaway | last post: by
8 posts views Thread by Olov Johansson | last post: by
5 posts views Thread by A.M | last post: by
5 posts views Thread by paulo | last post: by
15 posts views Thread by Nicholas M. Makin | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.