Convert boolean to a 1 or 0

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

    Convert boolean to a 1 or 0

    Hi All,

    I want to cleanly convert a boolean value to a bit value (1 or 0) in vb.net.

    I can:

    Math.Abs(CInt(b olValue))

    or:

    cint(bolValue) * -1

    However, I wondered if there was a built-in method.

    Thanks

    Alex



  • Chad Z. Hower aka Kudzu

    #2
    Re: Convert boolean to a 1 or 0

    "Alex Stevens" <alexStevens_no spam@gcc.co.uk> wrote in
    news:uJxZ2uP4DH A.1636@TK2MSFTN GP12.phx.gbl:[color=blue]
    > I want to cleanly convert a boolean value to a bit value (1 or 0) in
    > vb.net.
    >
    > I can:
    >
    > Math.Abs(CInt(b olValue))
    >
    > or:
    >
    > cint(bolValue) * -1
    >
    > However, I wondered if there was a built-in method.[/color]

    The cleanest method is to make a function that does it. It will make your
    code more readable as well.

    Or does VB still have the IIF function?


    --
    Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
    "Programmin g is an art form that fights back"


    ELKNews - Get your free copy at http://www.atozedsoftware.com

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Convert boolean to a 1 or 0

      * "Chad Z. Hower aka Kudzu" <cpub@hower.org > scripsit:[color=blue]
      > The cleanest method is to make a function that does it. It will make your
      > code more readable as well.
      >
      > Or does VB still have the IIF function?[/color]

      Yes, 'IIf' still exists.

      OT: Nice X-Face.

      --
      Herfried K. Wagner [MVP]
      <http://www.mvps.org/dotnet>

      Comment

      • Chad Z. Hower aka Kudzu

        #4
        Re: Convert boolean to a 1 or 0

        hirf-spam-me-here@gmx.at (Herfried K. Wagner [MVP]) wrote in news:
        #Ejsk5Q4DHA.216 8@TK2MSFTNGP12. phx.gbl:[color=blue][color=green]
        >> Or does VB still have the IIF function?[/color]
        >
        > Yes, 'IIf' still exists.[/color]

        Use IIF then. :)


        --
        Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
        "Programmin g is an art form that fights back"


        ELKNews - Get your free copy at http://www.atozedsoftware.com

        Comment

        • Tom Leylan

          #5
          Re: Convert boolean to a 1 or 0

          "Chad Z. Hower aka Kudzu" <cpub@hower.org > wrote...[color=blue]
          > hirf-spam-me-here@gmx.at (Herfried K. Wagner [MVP]) wrote in news:
          > #Ejsk5Q4DHA.216 8@TK2MSFTNGP12. phx.gbl:[color=green][color=darkred]
          > >> Or does VB still have the IIF function?[/color]
          > >
          > > Yes, 'IIf' still exists.[/color]
          >
          > Use IIF then. :)[/color]

          As an alternative (and in my mind a "cleaner" alternative) you may want to
          consider defining a new datatype. A structure should be able to handle the
          basic "Boolean" behavior and provide a "ToInteger" method as well.

          Tom


          Comment

          Working...