473,385 Members | 1,483 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,385 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 1551
?: 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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.