Ternary operator



  • 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)



  • @liserdarts said:

    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.



  • "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.



  • 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. 



  • 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"



  • 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".

     



  • 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.



  • @Welbog said:

    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. 

     

     



  • 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.



  • You registered to post this? Where's the anger, man?

    parameter.Value = (t == null ? DBNull.Value : t);



  • @Welbog said:

    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. 



  • 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. 



  • 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 🙂

     



  • @PSWorx said:

    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. 



  • @KattMan said:

    @PSWorx said:

    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



  • @PSWorx said:

    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. 



  • I was actually talking about VB (6.0), not .NET. But I wasn't aware they added return and short cirquiting in .NET. Sorry.



  • @webzter said:

    @KattMan said:
    @PSWorx said:

    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. 



  • @KattMan said:

    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 said:

    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....



  • @PSWorx said:

    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. 


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.