Cast Integer as Boolean?

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

    Cast Integer as Boolean?

    Hello All,

    How can I cast a System.Int32 as a boolean in VB.NET?

    I know I used to used Cbool(myInt) in VB6, but I cxan't seem to find the
    .NET equivalent.

    -Joel
  • David A. Osborn

    #2
    Re: Cast Integer as Boolean?

    Dim temp As Int32 = 1
    Dim temp2 As Boolean
    temp2 = System.Convert. ToBoolean(temp)

    "Joel Whitehouse" <joelwhitehouse @gmail.com> wrote in message
    news:O%23vRmoTm FHA.3828@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > Hello All,
    >
    > How can I cast a System.Int32 as a boolean in VB.NET?
    >
    > I know I used to used Cbool(myInt) in VB6, but I cxan't seem to find the
    > .NET equivalent.
    >
    > -Joel
    >[/color]



    Comment

    • Joel Whitehouse

      #3
      Re: Cast Integer as Boolean?

      David A. Osborn wrote:
      [color=blue]
      > Dim temp As Int32 = 1
      > Dim temp2 As Boolean
      > temp2 = System.Convert. ToBoolean(temp)
      >[/color]

      Thanks so much for your help! Have a great day!

      -Joel

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: Cast Integer as Boolean?

        Joel,

        Don't mixup in VBNet the single System namespace with .Net
        The framework has more things than that.
        By instance the Microsoft.Visua lBasic namespace

        The correct (most optimized) VBNet notation is:

        Dim IsFalse As Boolean = CBool(New System.Int32)

        I hope this helps,

        Cor


        Comment

        • m.posseth

          #5
          Re: Cast Integer as Boolean?

          Hello All,

          well this is probably some gassoline on this thread but
          i doubt that this is a true statement[color=blue]
          > The correct (most optimized) VBNet notation is: Dim IsFalse As Boolean =
          > CBool(New System.Int32)[/color]


          why should

          this be (more optimized)
          IsFalse = CBool(test)
          as this
          IsFalse = System.Convert. ToBoolean(test)
          or this
          IsFalse = CType(test, Boolean)

          isn`t it so that this are just different ways of doing the same ,, will it
          not produce the same IL code ?

          ps.

          Cor your example will always return false ,,, so it would be even more
          optimized to define it as a false boolean contstant ;-)


          Regards

          Michel Posseth [MCP]



          "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
          news:O7XqOPYmFH A.1148@TK2MSFTN GP12.phx.gbl...[color=blue]
          > Joel,
          >
          > Don't mixup in VBNet the single System namespace with .Net
          > The framework has more things than that.
          > By instance the Microsoft.Visua lBasic namespace
          >
          > The correct (most optimized) VBNet notation is:
          >
          > Dim IsFalse As Boolean = CBool(New System.Int32)
          >
          > I hope this helps,
          >
          > Cor
          >
          >[/color]


          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Cast Integer as Boolean?

            Michael,
            [color=blue]
            > isn`t it so that this are just different ways of doing the same ,, will it
            > not produce the same IL code ?
            >[/color]

            If there is no more optimizing possible, than there is probably no more
            optimizing the VisualBasic Convert functions.

            However is it than not still the most optimized one, all was it alone that
            somebody else had taken the time to check for that?

            :-)))

            Cor


            Comment

            • m.posseth

              #7
              Re: Cast Integer as Boolean?

              ofcourse there are always ways to optimize code ,, in the code flow for
              instance , however in builtin method \ function calls i doubt that


              are you telling me that somebody benchmarked this ??? where can i find this
              just for my interest ?

              regards

              Michel




              "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
              news:%23K%23Uw6 ZmFHA.1044@tk2m sftngp13.phx.gb l...[color=blue]
              > Michael,
              >[color=green]
              >> isn`t it so that this are just different ways of doing the same ,, will
              >> it not produce the same IL code ?
              >>[/color]
              >
              > If there is no more optimizing possible, than there is probably no more
              > optimizing the VisualBasic Convert functions.
              >
              > However is it than not still the most optimized one, all was it alone that
              > somebody else had taken the time to check for that?
              >
              > :-)))
              >
              > Cor
              >[/color]


              Comment

              • Armin Zingler

                #8
                Re: Cast Integer as Boolean?

                "m.posseth" <michelp@nohaus ystems.nl> schrieb[color=blue]
                > ofcourse there are always ways to optimize code ,, in the code flow
                > for instance , however in builtin method \ function calls i doubt
                > that
                >
                >
                > are you telling me that somebody benchmarked this ??? where can i
                > find this just for my interest ?[/color]



                CBool and CType are keywords, not function calls. They are (IL-)compiled
                inline, at least using Boolean as destination and Integer as the source data
                type. CBool and CType behave excactly the same in this case (CBool is
                shorter). The other one is a function call. Depending on the optimization
                settings, the functin call might also be (JIT-)compiled inline (I love this
                :-) ). In this case it is done for System.Convert. ToInt32:


                isfalse = CBool(test)
                00000022 test eax,eax
                00000024 setne bl
                00000027 movzx ebx,bl

                isfalse = System.Convert. ToBoolean(test)
                00000060 test eax,eax
                00000062 setne bl
                00000065 movzx ebx,bl


                isfalse = CType(test, Boolean)
                0000009e test eax,eax
                000000a0 setne bl
                000000a3 movzx ebx,bl

                So we never know what's actually fastest. ;-)


                Armin

                Comment

                • Armin Zingler

                  #9
                  Re: Cast Integer as Boolean?

                  "Armin Zingler" <az.nospam@free net.de> schrieb[color=blue]
                  > :-) ). In this case it is done for System.Convert. ToInt32:[/color]

                  .....done for System.Convert. ToBoolean



                  Armin



                  Comment

                  • Cor Ligthert [MVP]

                    #10
                    Re: Cast Integer as Boolean?

                    Michel,

                    Do you know this page?



                    And than about this in the section
                    Conversion Functions, CType, DirectCast, and System.Convert

                    Cor


                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: Cast Integer as Boolean?

                      Joel,
                      As Cor & Armin suggests,

                      | How can I cast a System.Int32 as a boolean in VB.NET?
                      CBool is a valid .NET VB.NET keyword.

                      Convert.ToBoole an will always result in a call to another routine (that the
                      JIT may or may not inline).

                      CBool(Int32) is an IL statement.

                      For details of using CBool verses calling Convert.ToBoole an see:



                      www.panopticoncentral.net is Paul Vick's blog, Paul Vick is the Technical
                      Lead of the Visual Basic.NET Development team at Microsoft.

                      Hope this helps
                      Jay



                      "Joel Whitehouse" <joelwhitehouse @gmail.com> wrote in message
                      news:O%23vRmoTm FHA.3828@TK2MSF TNGP12.phx.gbl. ..
                      | Hello All,
                      |
                      | How can I cast a System.Int32 as a boolean in VB.NET?
                      |
                      | I know I used to used Cbool(myInt) in VB6, but I cxan't seem to find the
                      | .NET equivalent.
                      |
                      | -Joel


                      Comment

                      • m.posseth

                        #12
                        Re: Cast Integer as Boolean?

                        Thanks Armin ,,,


                        for the clear explanation




                        "Armin Zingler" <az.nospam@free net.de> wrote in message
                        news:uokcRhamFH A.3144@TK2MSFTN GP12.phx.gbl...[color=blue]
                        > "Armin Zingler" <az.nospam@free net.de> schrieb[color=green]
                        >> :-) ). In this case it is done for System.Convert. ToInt32:[/color]
                        >
                        > ....done for System.Convert. ToBoolean
                        >
                        >
                        >
                        > Armin
                        >
                        >
                        >[/color]


                        Comment

                        • m.posseth

                          #13
                          Re: Cast Integer as Boolean?

                          No i had not yet read this page ,,, now i did ... thank you for pointing me
                          at it ,, it had some verry interesting info

                          now i know for sure that

                          IsFalse = CBool(test)
                          and
                          IsFalse = CType(test, Boolean)

                          produce the same IL code

                          if you meant with "most optimized" the shortest way of coding the fastest
                          method you are right ,,, but i might have misunderstood that for "the one
                          and only fastest method in code execution " this is were my doubts were

                          regards ( groeten )

                          Michel Posseth






                          "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
                          news:%23Eh%230m amFHA.1416@TK2M SFTNGP09.phx.gb l...[color=blue]
                          > Michel,
                          >
                          > Do you know this page?
                          >
                          > http://msdn.microsoft.com/library/de...tinternals.asp
                          >
                          > And than about this in the section
                          > Conversion Functions, CType, DirectCast, and System.Convert
                          >
                          > Cor
                          >
                          >[/color]


                          Comment

                          Working...