Fave Operator of the Day
-
The C# 2.0 null coalescing operator
??
God bless its little cotton socks.cheers, Chris Maunder
CodeProject.com : C++ MVP
-
The C# 2.0 null coalescing operator
??
God bless its little cotton socks.cheers, Chris Maunder
CodeProject.com : C++ MVP
You tease - don't blue-ball us, give up an example!!! :)
¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
-
You tease - don't blue-ball us, give up an example!!! :)
¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
Jim Crafton wrote:
You tease - don't blue-ball us, give up an example!!!
return _cachedItem ?? (_cachedItem = GetItem());
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com link -
Jim Crafton wrote:
You tease - don't blue-ball us, give up an example!!!
return _cachedItem ?? (_cachedItem = GetItem());
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com linkIs this just a C# thing or is there a VB equivalent (I'm almost afraid to ask what that monstrosity would look like)?
¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
-
Is this just a C# thing or is there a VB equivalent (I'm almost afraid to ask what that monstrosity would look like)?
¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
Jim Crafton wrote:
Is this just a C# thing or is there a VB equivalent (I'm almost afraid to ask what that monstrosity would look like)?
I don't think there's a VB equivalent (though I don't know for sure).
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com link -
The C# 2.0 null coalescing operator
??
God bless its little cotton socks.cheers, Chris Maunder
CodeProject.com : C++ MVP
Personally, i'd have preferred something along the lines of the GNU C "shortcut ternary" operator (which it closely resembles). But then, i'd have liked it if C# interpreted
null
values asfalse
instead of requiring an explicit comparison... :->----
...the wind blows over it and it is gone, and its place remembers it no more...
-
Is this just a C# thing or is there a VB equivalent (I'm almost afraid to ask what that monstrosity would look like)?
¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
Jim Crafton wrote:
Is this just a C# thing or is there a VB equivalent (I'm almost afraid to ask what that monstrosity would look like)?
Monstrosity you say? In VB? Unthinkable! :rolleyes: Classic VB (and VB.NET) has a ternary operator... except, it isn't really an operator. The IIF() function takes three operands, if the first is true it returns the second, otherwise it returns the third. Unlike the C++/C# ternary operator, this will always evaluate all three expressions (being a function call rather than an operator, it has to). This provided yet another pitfall when using VB, as expressions such as:
someVar = IIF(boolVar, HorriblyDestructiveCall1(), EvenMoreDestructiveCall())
...would end up trashing whatever global state you were manipulating with the two functions twice, once for each call (remember, this is VB - of course there's a horrible, fragile, global state of some sort). VB9 now provides a true ternary operator - If. So you can write:someVar = If(boolVar, HorriblyDestructiveCall1(), EvenMoreDestructiveCall())
... and only one of the possible functions will be called. Or, for the null coalescing version:someVar = If(possiblyNothing, BetterThanNothing())
Good times...----
...the wind blows over it and it is gone, and its place remembers it no more...
-
The C# 2.0 null coalescing operator
??
God bless its little cotton socks.cheers, Chris Maunder
CodeProject.com : C++ MVP
Yeah, I love that one.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Personally, i'd have preferred something along the lines of the GNU C "shortcut ternary" operator (which it closely resembles). But then, i'd have liked it if C# interpreted
null
values asfalse
instead of requiring an explicit comparison... :->----
...the wind blows over it and it is gone, and its place remembers it no more...
yeah, I still from time to time type if (myobj) and then remember.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Is this just a C# thing or is there a VB equivalent (I'm almost afraid to ask what that monstrosity would look like)?
¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
-
The C# 2.0 null coalescing operator
??
God bless its little cotton socks.cheers, Chris Maunder
CodeProject.com : C++ MVP
-
The C# 2.0 null coalescing operator
??
God bless its little cotton socks.cheers, Chris Maunder
CodeProject.com : C++ MVP
jmp
Allows you to jump from function to function :)xacc.ide
IronScheme a R5RS-compliant Scheme on the DLR
The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach." -
Jim Crafton wrote:
You tease - don't blue-ball us, give up an example!!!
return _cachedItem ?? (_cachedItem = GetItem());
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com linkI would have like a
??=
operator too, for exactly what you are showing. Personally, I find it more handy with dealing with optionals.xacc.ide
IronScheme a R5RS-compliant Scheme on the DLR
The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach." -
and proud! :cool:
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook "There is no wealth like knowledge, no poverty like ignorance." Ali ibn Abi Talib "Animadvertistine, ubicumque stes, fumum recta in faciem ferri?"
-
You tease - don't blue-ball us, give up an example!!! :)
¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
Also easy when working with strings. string result = someString ?? string.Empty;
-
The C# 2.0 null coalescing operator
??
God bless its little cotton socks.cheers, Chris Maunder
CodeProject.com : C++ MVP
Aha.. so when is CodeProject being upgrade to 3.5 then ? ;)
'Howard
-
The C# 2.0 null coalescing operator
??
God bless its little cotton socks.cheers, Chris Maunder
CodeProject.com : C++ MVP
For the really nerdy:
string value = value1 ?? value2 ?? value3 ?? finalDefaultValue;
Deja View - the feeling that you've seen this post before.
-
For the really nerdy:
string value = value1 ?? value2 ?? value3 ?? finalDefaultValue;
Deja View - the feeling that you've seen this post before.
OK. For us poor, ignorant C++ jocks, please explain what that abomination does. I'm guessing it's something like this:
if (value1 != NULL) value = value1;
else if (value2 != NULL) value = value2;
else if (value3 != NULL) value = value3;
else value = finalDefaultValue;
Software Zen:
delete this;
-
OK. For us poor, ignorant C++ jocks, please explain what that abomination does. I'm guessing it's something like this:
if (value1 != NULL) value = value1;
else if (value2 != NULL) value = value2;
else if (value3 != NULL) value = value3;
else value = finalDefaultValue;
Software Zen:
delete this;
Yup - that's exactly what it does - it coalesces through the chain until it finds a none-null value.
Deja View - the feeling that you've seen this post before.
-
Yup - that's exactly what it does - it coalesces through the chain until it finds a none-null value.
Deja View - the feeling that you've seen this post before.
I'm curious. Does this pattern occur often enough in .NET programming that it was worth adding an operator for it? Or is it the case that, since the operator is available, you use that pattern more often?
Software Zen:
delete this;