iif and type inference

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

    iif and type inference

    Hello,

    Is there a way in VB.NET to do something like

    dim myInt = if foo() then bar(3) else 4

    and have the compiler infer that myInt is an Integer, since bar()
    returns an integer and 4 is an integer?

    Thanks a lot for any info.

    Maurizio
  • Stanimir Stoyanov

    #2
    Re: iif and type inference

    No, there is not a short equivalent of if-else in VB.NET as there is in C#
    (condition ? true : false) but if you are sure that both statements return
    an object of the same type, you can cast the result of IIf: CInt(IIf(foo(),
    bar(3), 4)) in your case.

    Best Regards,
    Stanimir Stoyanov | www.stoyanoff.info

    "Maurizio Colucci" <maurizio.coluc ci@gmail.comwro te in message
    news:606ab28b-84cd-4263-b1f9-a3879a3f14da@27 g2000hsf.google groups.com...
    Hello,
    >
    Is there a way in VB.NET to do something like
    >
    dim myInt = if foo() then bar(3) else 4
    >
    and have the compiler infer that myInt is an Integer, since bar()
    returns an integer and 4 is an integer?
    >
    Thanks a lot for any info.
    >
    Maurizio

    Comment

    • Martin H.

      #3
      Re: iif and type inference

      Hello Maurizio,

      Since the code is not sooo difficult - why don't you just give it a try?

      Best regards,

      Martin

      On 17.07.2008 16:40, wrote Maurizio Colucci:
      Hello,
      >
      Is there a way in VB.NET to do something like
      >
      dim myInt = if foo() then bar(3) else 4
      >
      and have the compiler infer that myInt is an Integer, since bar()
      returns an integer and 4 is an integer?
      >
      Thanks a lot for any info.
      >
      Maurizio

      Comment

      • Maurizio Colucci

        #4
        Re: iif and type inference

        On Jul 17, 12:14 pm, "Martin H." <hk...@gmx.netw rote:
        Hello Maurizio,
        >
        Since the code is not sooo difficult - why don't you just give it a try?
        >
        I'm sorry, I don't think I understand that you mean.

        Maurizio

        Comment

        • Maurizio Colucci

          #5
          Re: iif and type inference

          On Jul 17, 12:08 pm, "Stanimir Stoyanov" <stoya...@live. comwrote:
          No, there is not a short equivalent of if-else in VB.NET as there is in C#
          (condition ? true : false) but if you are sure that both statements return
          an object of the same type, you can cast the result of IIf: CInt(IIf(foo(),
          bar(3), 4)) in your case.
          Thank you, I was afraid that was the only way. It also does not use
          lazy evaluation.

          Maurizio

          Comment

          • Patrice

            #6
            Re: iif and type inference

            Which version ? If VB 2008, you could use the new If function :

            Dim myInt = If(foo(), bar(3), 4)

            --
            Patrice

            "Maurizio Colucci" <maurizio.coluc ci@gmail.coma écrit dans le message de
            groupe de discussion :
            606ab28b-84cd-4263-b1f9-a3879a3f14da...le groups.com...
            Hello,
            >
            Is there a way in VB.NET to do something like
            >
            dim myInt = if foo() then bar(3) else 4
            >
            and have the compiler infer that myInt is an Integer, since bar()
            returns an integer and 4 is an integer?
            >
            Thanks a lot for any info.
            >
            Maurizio

            Comment

            • Maurizio Colucci

              #7
              Re: iif and type inference

              On Jul 17, 1:15 pm, "Patrice" <http://www.chez.com/scribe/wrote:
              Which version ? If VB 2008, you could use the new If function :
              >
                      Dim myInt = If(foo(), bar(3), 4)
              >
              --
              Patrice
              That's great! Exactly what I was hoping for. Thanks a lot Patrice.

              M

              Comment

              • Maurizio Colucci

                #8
                Re: iif and type inference


                Thank you Nick. That's exactly what I was hoping for. :)

                Maurizio

                Comment

                Working...