|
Ternary operator
Last post 07-26-2007 6:04 PM by ammoQ. 35 replies.
-
-
-
Morbii


- Joined on 10-21-2006
- Posts 119
|
liserdarts:
I’ve heard people discourage using a ternary operator. But I’ve never seen anybody go out of their way to use it.
Return IIF(UCase(Data.Web_enabled) = "Y", True, False)
Alrighty then.
|
|
-
-
sootzoo


- Joined on 02-12-2007
- Posts 171
|
"IIf" doesn't even work like an actual ternary operator ala C - both true and false cases get evaluated in VB, which can be seriously annoying.
All in all, this is either the stupidest thing I've read all week (and I'm porting SSDS to .NET!), or the worst trolling attempt ever. -bstorer
|
|
-
-
KattMan


- Joined on 10-18-2006
- Posts 364
|
yes and purely due to the ambiguity of the overloaded = operator. It could have been written as such: Return UCase(Data.Web_enabled) = "Y" But how many vb programmers understand if that is now an assignment or an equality operation? FWIW, It is an equality operator in this context, not an assignment.
CAPTCHA? We ain't got no CAPTCHA. We don't need no CAPTCHA. We don't need no stinking CAPTCHA!
|
|
-
-
Albatross


- Joined on 10-31-2005
- Calgary, AB
- Posts 307
|
Whenever I return a value like that I always put brackets around the condition, like:
Return ( UCase(Data.Web_enabled) = "Y" )
It makes it a little more readable, and I can make sure that it won't get turned into an assignment by removing the "return"
It's funny because it's out of context.
|
|
-
-
KattMan


- Joined on 10-18-2006
- Posts 364
|
well in this case it never will become an assignment because of the UCase() Now if instead it looked like the following: Return Data.Web_enabled = "Y" It would be a comparison, but if the return is removed it would become an assignment. Placing parenthesis as you suggest around it would make it a comparison always. The rules behind this are pretty straight forward, but the application of it is not. You have to take into account "implied assignments".
CAPTCHA? We ain't got no CAPTCHA. We don't need no CAPTCHA. We don't need no stinking CAPTCHA!
|
|
-
-
Welbog


- Joined on 02-08-2007
- Posts 434
|
I love the ternary operator so much, and VB makes me sad because IIF just isn't the ternary operator, and is pretty much completely useless as in the situations in which ?: is most useful, IIF just doesn't cut it.
t = (t == null)?(""):(t);
Is just so much better than
t = CType(IFF(t Is Nothing,"",t),String)
VB makes me sad.
|
|
-
-
KattMan


- Joined on 10-18-2006
- Posts 364
|
Welbog:I love the ternary operator so much, and VB makes me sad because IIF just isn't the ternary operator, and is pretty much completely useless as in the situations in which ?: is most useful, IIF just doesn't cut it.
t = (t == null)?(""):(t);
Is just so much better than
t = CType(IFF(t Is Nothing,"",t),String)
VB makes me sad.
I think maybe because you do not understand it. t = (t == null)?(""):(t);
will return either "" or the contents of t t = CType(IFF(t Is Nothing,"",t),String)
Is superfluous instead use this: t = IFF(t Is Nothing,"",t) It returns either "" or the contents of t SO IFF(t Is Nothing,"",t) is equal to (t == null)?(""):(t) Now if t was not a string variable you could run into problems with both of these if it actually held data. In the C version you would have to cast it to a string the same way you tried to convert it in the VB version. Let's not add complexity to one without showing the comparable checks on the other.
CAPTCHA? We ain't got no CAPTCHA. We don't need no CAPTCHA. We don't need no stinking CAPTCHA!
|
|
-
-
Welbog


- Joined on 02-08-2007
- Posts 434
|
t = IFF(t Is Nothing, "", t) won't work because IFF returns a type Object, but t is a String. VB won't cast that for you, so you have to cast it (unless you're using that terrible option which makes VB basically loosely typed). However, ?: returns whatever object type are in the last two arguments, without having to cast from Object (at least in Java, of which I am most familiar).
That's why I added the explicit cast in the VB version but not the Java version.
|
|
-
-
webzter


- Joined on 11-10-2006
- Minneapolis, MN USA
- Posts 202
|
You registered to post this? Where's the anger, man?
parameter.Value = (t == null ? DBNull.Value : t);
|
|
-
-
KattMan


- Joined on 10-18-2006
- Posts 364
|
Welbog:t = IFF(t Is Nothing, "", t) won't work because IFF returns a type Object, but t is a String. VB won't cast that for you, so you have to cast it (unless you're using that terrible option which makes VB basically loosely typed). However, ?: returns whatever object type are in the last two arguments, without having to cast from Object (at least in Java, of which I am most familiar).
That's why I added the explicit cast in the VB version but not the Java version.
Flipped back to VB.net and made sure Option strict was on (it was) and tested this. You are correct, under this setting it won't cast. Bugger! You can get the results you want, but you do have to take the extra step of casting. I guess because IIF() is a function and it has to return a particular type. So yeah it is a ternary function, not a ternary operator. So I stand corrected.
CAPTCHA? We ain't got no CAPTCHA. We don't need no CAPTCHA. We don't need no stinking CAPTCHA!
|
|
-
-
KattMan


- Joined on 10-18-2006
- Posts 364
|
I guess the simplest form would actually be the following: CStr(IFF(t Is Nothing,"",t)) Still not as clean as I would like, but gets close.
CAPTCHA? We ain't got no CAPTCHA. We don't need no CAPTCHA. We don't need no stinking CAPTCHA!
|
|
-
-
PSWorx


- Joined on 04-28-2006
- Posts 713
|
The problem in VB is that IIf() is nothing more than a simple function. So it can't magically control parameter evaluation or change its return type like operators or statements do. Or in fact don't in VB, which shows another major annoyance: No short-cirquiting boolean operators...
by the way, Return is no valid VB as far as I know (except if Return() is a Method/Function). The correct syntax would be:
Function MyFunction(...) ... MyFunction = Data.Web_enabled = "Y" ... End Function
Which might give you a hint why it isn't done that way ;)
By the way, for even more VB fun: The the name of the function you're inside ("MyFunction") is fully treated like a local variable. That meanst that constructs like
Function Fac(n as Integer) as Long Dim I as Integer Fac = 1 For I = 2 To n Fac = Fac * I Next I End Function
Are valid and possible. Oh, of course VB supports recursive function calls as well :)
|
|
-
-
KattMan


- Joined on 10-18-2006
- Posts 364
|
PSWorx:Or in fact don't in VB, which shows another major annoyance: No short-cirquiting boolean operators...
Actually you can short-circuit evaluations. This is what the ANDALSO and ORELSE operators are for. I know I don't like them either but short-circuiting in VB is possible.
CAPTCHA? We ain't got no CAPTCHA. We don't need no CAPTCHA. We don't need no stinking CAPTCHA!
|
|
-
-
webzter


- Joined on 11-10-2006
- Minneapolis, MN USA
- Posts 202
|
KattMan: PSWorx:
Or in fact don't in VB, which shows another major annoyance: No short-cirquiting boolean operators...
Actually you can short-circuit evaluations.
This is what the ANDALSO and ORELSE operators are for. I know I don't like them either but short-circuiting in VB is possible.
AFAIK, IANAVBLA, VB didn't support short circuiting... VB.NET does
|
|
-
-
KattMan


- Joined on 10-18-2006
- Posts 364
|
PSWorx:by the way, Return is no valid VB as far as I know (except if Return() is a Method/Function).
Return is valid in VB, but it has to return the type the the function is defined as returning. It is also only valid with a right hand value in "functions" as "subs" have no return value (or rather are functions that return void) so they just use Return. But you are forgiven if you do not normally code in VB.
CAPTCHA? We ain't got no CAPTCHA. We don't need no CAPTCHA. We don't need no stinking CAPTCHA!
|
|
-
-
-
KattMan


- Joined on 10-18-2006
- Posts 364
|
webzter: KattMan: PSWorx:
Or in fact don't in VB, which shows another major annoyance: No short-cirquiting boolean operators...
Actually you can short-circuit evaluations.
This is what the ANDALSO and ORELSE operators are for. I know I don't like them either but short-circuiting in VB is possible.
AFAIK, IANAVBLA, VB didn't support short circuiting... VB.NET does
But looking at the rest of the statements, we are talking VB.Net here. VB Classic does not have an Option Strict just an Option Explicit which does allow the automatic casting of return values. Of course I can't check that right now, don't have VB6 available to me. Of course this recalls the question, since the languages are so different why we even have VB.Net instead of some other name. Then again, why not just C#? I know, I know, many vb classic developers felt like they were going to get short shafted.
CAPTCHA? We ain't got no CAPTCHA. We don't need no CAPTCHA. We don't need no stinking CAPTCHA!
|
|
-
-
webzter


- Joined on 11-10-2006
- Minneapolis, MN USA
- Posts 202
|
KattMan:But looking at the rest of the statements, we are talking VB.Net here. VB Classic does not have an Option Strict just an Option Explicit which does allow the automatic casting of return values. Of course I can't check that right now, don't have VB6 available to me.
Of course this recalls the question, since the languages are so different why we even have VB.Net instead of some other name. Then again, why not just C#? I know, I know, many vb classic developers felt like they were going to get short shafted.
PSWorx:I was actually talking about VB (6.0), not .NET. But I wasn't aware they added return and short cirquiting in .NET. Sorry.
To which I say, bring back Fred.NET!
It's funny... I wrote a book that was (sort of) on VB.NET and I can't remember enough of it to code my way out of a wet paper bag. I get to help out occasionally with some of the other groups here that do VB.NET and it ends up being a bit of a mental exercise just to remember what goes where....
|
|
-
-
KattMan


- Joined on 10-18-2006
- Posts 364
|
PSWorx:By the way, for even more VB fun: The the name of the function you're inside ("MyFunction") is fully treated like a local variable. That meanst that constructs like
Function Fac(n as Integer) as Long Dim I as Integer Fac = 1 For I = 2 To n Fac = Fac * I Next I End Function
Are valid and possible. Oh, of course VB supports recursive function calls as well :)
Your post is giving me loads of fun, and no I'm not picking on you. Yes the name of the function is seen as a local variable, and it also supports recursion. Which leads to funky things like the following: Function MyFunct(MyInt as Int32) as Int32 If MyInt < 10 Then MyFunc = MyFunc(MyInt) End If End Function Try debugging stuff like that one day.
CAPTCHA? We ain't got no CAPTCHA. We don't need no CAPTCHA. We don't need no stinking CAPTCHA!
|
|
-
-
PSWorx


- Joined on 04-28-2006
- Posts 713
|
KattMan:Your post is giving me loads of fun, and no I'm not picking on you.
Hehe, thanks very much. KattMan:Yes the name of the function is seen as a local variable, and it also supports recursion. Which leads to funky things like the following: Function MyFunct(MyInt as Int32) as Int32 If MyInt < 10 Then MyFunc = MyFunc(MyInt) End If End Function Try debugging stuff like that one day.
Correct me if I'm wrong, but isn't the even more insane variant of MyFunc = MyFunc(MyFunc) valid as well?
|
|
-
-
webzter


- Joined on 11-10-2006
- Minneapolis, MN USA
- Posts 202
|
|
-
-
-
KattMan


- Joined on 10-18-2006
- Posts 364
|
PSWorx: KattMan:Your post is giving me loads of fun, and no I'm not picking on you.
Hehe, thanks very much. KattMan:Yes the name of the function is seen as a local variable, and it also supports recursion. Which leads to funky things like the following: Function MyFunct(MyInt as Int32) as Int32 If MyInt < 10 Then MyFunc = MyFunc(MyInt) End If End Function Try debugging stuff like that one day.
Correct me if I'm wrong, but isn't the even more insane variant of MyFunc = MyFunc(MyFunc) valid as well?
Yes it would be but in that example the parameter to the function would always be 0 and hence never end. My example actually does end. Of course you could have assigned MyFunc = MyInt right before that in order to get your example working without running out of stack space.
CAPTCHA? We ain't got no CAPTCHA. We don't need no CAPTCHA. We don't need no stinking CAPTCHA!
|
|
-
-
XIU


- Joined on 01-08-2007
- Posts 134
|
Welbog:t = (t == null)?(""):(t);
C# 2.0 is even easier: t = t ?? ""; The ?? operator checks if the argument before it is null, if it is it will use the argument after it.
|
|
-
|
|