473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1552
?: 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
26
by: Ney André de Mello Zunino | last post by:
Hello. I have noticed, in a lot of C and C++ code, that many programmers seem to prefer putting the test values first in conditional expressions. I.e., they would rather write this: if (-1 ==...
62
by: Reinhold Birkenfeld | last post by:
Hi, after Guido's pronouncement yesterday, in one of the next versions of Python there will be a conditional expression with the following syntax: X if C else Y which is the same as today's...
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
6
by: Chris Dunaway | last post by:
Consider this code (.Net 2.0) which uses a nullable type: private void button1_Click(object sender, System.EventArgs e) { DateTime? nullableDate; nullableDate = (condition) ? null :...
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
11
by: volcs0 | last post by:
I've done this in Scheme, but I'm not sure I can in Python. I want the equivalent of this: if a == "yes": answer = "go ahead" else: answer = "stop" in this more compact form:
5
by: A.M | last post by:
Hi, I am using Python 2.4. I read the PEP 308 at: http://www.python.org/dev/peps/pep-0308/
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
15
by: Nicholas M. Makin | last post by:
I was just thinking that I understood the conditional operator when I coded the following expecting it to fail: int a= 10, b= 20, c= 0; ((a < b) ? a : b) = c; // a=0 a=20; b= 10; ((a < b) ? a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.