IIF with Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shawn

    #1

    IIF with Array

    OK, Please tell me if I am doing something wrong here...

    This does NOT work if args.Length = 0 (It gives an
    IndexOutOfRange Exception on the args() array)
    Public Shared Sub Main(ByVal args() As String)
    Dim val As String = IIf(args.Length 0, args(0).ToStrin g,
    "10")
    Console.WriteLi ne("Waiting for workflow completion ({0}
    seconds...).", val)
    End Sub

    This DOES work if args.Length = 0
    Public Shared Sub Main(ByVal args() As String)
    Dim val As String
    If args.Length 0 Then
    val = args(0)
    Else
    val = "10"
    End If
    Console.WriteLi ne("Waiting for workflow completion ({0}
    seconds...).", val)
    End Sub

    Am I wrong to assume that IIF can be used this way?

    Thanks,
    Shawn
  • Stephany Young

    #2
    Re: IIF with Array

    Yes!

    To use IIf, both the TruePart and the FalsePart MUST be evaluatable.

    In the case of args.Length = 0 then args(0).ToStrin g cannot be evaluated
    because the array, (args), does not have any elements. IIf thefore,
    correctly, throws an exception.

    And, yes, (before anyone jumps down my throat), I did read that VB.Net 2008
    has a 'proper' ternary operator.


    "Shawn" <shawngentry@gm ail.comwrote in message
    news:a11ada30-f5c3-4ff2-8395-76e8cbc0423d@d2 1g2000prf.googl egroups.com...
    OK, Please tell me if I am doing something wrong here...
    >
    This does NOT work if args.Length = 0 (It gives an
    IndexOutOfRange Exception on the args() array)
    Public Shared Sub Main(ByVal args() As String)
    Dim val As String = IIf(args.Length 0, args(0).ToStrin g,
    "10")
    Console.WriteLi ne("Waiting for workflow completion ({0}
    seconds...).", val)
    End Sub
    >
    This DOES work if args.Length = 0
    Public Shared Sub Main(ByVal args() As String)
    Dim val As String
    If args.Length 0 Then
    val = args(0)
    Else
    val = "10"
    End If
    Console.WriteLi ne("Waiting for workflow completion ({0}
    seconds...).", val)
    End Sub
    >
    Am I wrong to assume that IIF can be used this way?
    >
    Thanks,
    Shawn

    Comment

    • Cor Ligthert[MVP]

      #3
      OT: Re: IIF with Array

      You apparently are not using STRICT ON. I strongly urge you to. It
      makes you use some casts, but you can find a lot of bugs with it on.
      Yea, I don't understand why Armin never points people on this.

      (You have to be a very long time regular from this newsgroup to understand
      the meaning from what I wrote)

      :-)

      Cor

      Comment

      • Cor Ligthert[MVP]

        #4
        Re: IIF with Array

        >
        And, yes, (before anyone jumps down my throat), I did read that VB.Net
        2008 has a 'proper' ternary operator.
        >
        >
        However a ternary operator stays in my idea something that is hard to
        understand by maintaining and therefore in my idea more meant for scripting;
        Where that is standard difficult.

        Cor


        Comment

        • Michael C

          #5
          Re: IIF with Array

          "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
          news:03114FD1-C11F-4AFD-89C5-48927C91C8AD@mi crosoft.com...

          >And, yes, (before anyone jumps down my throat), I did read that VB.Net
          >2008 has a 'proper' ternary operator.
          >>
          >>
          However a ternary operator stays in my idea something that is hard to
          understand by maintaining and therefore in my idea more meant for
          scripting; Where that is standard difficult.
          As long as it's kept simple I don't see the problem:

          int x = (SomeCheck ? 1 : 0)

          Surely not too hard to maintain?

          Michael


          Comment

          • Stephany Young

            #6
            Re: IIF with Array

            What's to understand?

            If one understands the concept of If ... Then ... Else then one already
            understands the concept of a ternary operator.


            "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
            news:03114FD1-C11F-4AFD-89C5-48927C91C8AD@mi crosoft.com...

            >And, yes, (before anyone jumps down my throat), I did read that VB.Net
            >2008 has a 'proper' ternary operator.
            >>
            >>
            However a ternary operator stays in my idea something that is hard to
            understand by maintaining and therefore in my idea more meant for
            scripting; Where that is standard difficult.
            >
            Cor
            >
            >

            Comment

            • Cor Ligthert [MVP]

              #7
              Re: IIF with Array

              Michael,
              As long as it's kept simple I don't see the problem:
              >
              int x = (SomeCheck ? 1 : 0)
              >
              Surely not too hard to maintain?
              >
              You are rigth

              However, now this one and it seems that people to like it for things like
              this.

              TelBijBonus(Ver koper).Value = (Klant="Brabant " and Provincie <"Gelderland "
              And HeeftKabel And HeeftBetaald ? BerekenWinst :
              BerekenAanmanin g.ResultaatZero )

              I don't know it it is syntyntical complete however you understand probably
              what I mean?

              :-)

              Cor


              Comment

              • Cor Ligthert [MVP]

                #8
                Re: IIF with Array

                If one understands the concept of If ... Then ... Else then one already
                understands the concept of a ternary operator.
                >
                Try to find out what I did mean with my piece of sample code to Michael,

                When nicely done with an if then this is easy to read for me, this one I
                made is something I have to put in pieces first.

                However maybe it is me.


                Cor


                Comment

                • Michael C

                  #9
                  Re: IIF with Array

                  "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                  news:utJ6G3mNIH A.748@TK2MSFTNG P04.phx.gbl...
                  You are rigth
                  >
                  However, now this one and it seems that people to like it for things like
                  this.
                  >
                  TelBijBonus(Ver koper).Value = (Klant="Brabant " and Provincie <>
                  "Gelderland " And HeeftKabel And HeeftBetaald ? BerekenWinst :
                  BerekenAanmanin g.ResultaatZero )
                  >
                  I don't know it it is syntyntical complete however you understand probably
                  what I mean?
                  I've seen this arguement raised before. Just because something can be abused
                  does not mean that it will be abused and should therefore be removed. Pretty
                  much everything can be abused, even the standard if statement.

                  Michael


                  Comment

                  • Jay B. Harlow [MVP - Outlook]

                    #10
                    Re: IIF with Array

                    Shawn,
                    As the others have pointed out, IIf is a function; all arguments will be
                    evaluated, prior to VB 2008 I would use a generic IIf to get around the
                    casting problem.



                    Starting with VB 2008 I now use the If operator.

                    Dim val As String = If(args.Length 0, args(0).ToStrin g, "10")

                    There are two versions of the If operator. The 3 argument version above that
                    replaces the IIf, and a 2 argument version similar to IsNull in SQL Server.

                    http://msdn2.microsoft.com/en-us/lib...85(VS.90).aspx

                    NOTE: You can introduce a Generic IIf that has an Obsolete attribute

                    <Obsolete_
                    Public Function IIf(Of T)(ByVal expression As Boolean, _
                    ByVal truePart As T, ByVal falsePart As T) As T
                    If expression Then
                    Return truePart
                    Else
                    Return falsePart
                    End If
                    End Function

                    Which will highlight every place your using the function allowing you to
                    change over to the operator.

                    --
                    Hope this helps
                    Jay B. Harlow [MVP - Outlook]
                    ..NET Application Architect, Enthusiast, & Evangelist
                    T.S. Bradley - http://www.tsbradley.net


                    "Shawn" <shawngentry@gm ail.comwrote in message
                    news:a11ada30-f5c3-4ff2-8395-76e8cbc0423d@d2 1g2000prf.googl egroups.com...
                    OK, Please tell me if I am doing something wrong here...
                    >
                    This does NOT work if args.Length = 0 (It gives an
                    IndexOutOfRange Exception on the args() array)
                    Public Shared Sub Main(ByVal args() As String)
                    Dim val As String = IIf(args.Length 0, args(0).ToStrin g,
                    "10")
                    Console.WriteLi ne("Waiting for workflow completion ({0}
                    seconds...).", val)
                    End Sub
                    >
                    This DOES work if args.Length = 0
                    Public Shared Sub Main(ByVal args() As String)
                    Dim val As String
                    If args.Length 0 Then
                    val = args(0)
                    Else
                    val = "10"
                    End If
                    Console.WriteLi ne("Waiting for workflow completion ({0}
                    seconds...).", val)
                    End Sub
                    >
                    Am I wrong to assume that IIF can be used this way?
                    >
                    Thanks,
                    Shawn

                    Comment

                    Working...