Cannot convert type 'int' to 'bool'

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

    Cannot convert type 'int' to 'bool'

    The following code:

    int test = 1;
    bool isTrue = (bool)test;

    results in a compiler error:

    Cannot convert type 'int' to 'bool'

    wtf, any ideas on how to work around this?
  • Jonathan Wood

    #2
    Re: Cannot convert type 'int' to 'bool'

    I don't know why you get this error but it could be related with bool values
    being limited to 1 or 0. In this case, the int is 1, but I wouldn't want the
    compiler trying to determine stuff like that--too much overhead.

    How about something like this:

    bool isTrue = (test != 0);

    --
    Jonathan Wood
    SoftCircuits Programming



    "John" <no@spam.comwro te in message
    news:u9U4dJN$HH A.1900@TK2MSFTN GP02.phx.gbl...
    The following code:
    >
    int test = 1;
    bool isTrue = (bool)test;
    >
    results in a compiler error:
    >
    Cannot convert type 'int' to 'bool'
    >
    wtf, any ideas on how to work around this?

    Comment

    • John

      #3
      Re: Cannot convert type 'int' to 'bool'

      Jonathan Wood wrote:
      How about something like this:
      >
      bool isTrue = (test != 0);
      >
      Yes, it seems that their is no implicit conversion of int to bool,
      therefore the only recourse is to specifically use an equivalence
      operator. Oh well, must have something to do with the overhead of the
      JIT. Well, not that the JIT implicitly has overhead, it's just that we
      have to specifically spoon feed it exactly we want.

      Comment

      • Registered User

        #4
        Re: Cannot convert type 'int' to 'bool'

        On Fri, 21 Sep 2007 21:39:26 -0700, John <no@spam.comwro te:
        >The following code:
        >
        >int test = 1;
        >bool isTrue = (bool)test;
        >
        >results in a compiler error:
        >
        >Cannot convert type 'int' to 'bool'
        >
        >wtf, any ideas on how to work around this?
        bool isTrue = Convert.ToBoole an(test);

        You will find that non-zero integer arguments to Convert.ToBoole an all
        return true.

        regards
        A.G.

        Comment

        • =?Utf-8?B?U29tIE5hdGggU2h1a2xh?=

          #5
          RE: Cannot convert type 'int' to 'bool'

          check it its working
          int k= 1;
          bool b = Convert.ToBoole an(k);

          "John" wrote:
          The following code:
          >
          int test = 1;
          bool isTrue = (bool)test;
          >
          results in a compiler error:
          >
          Cannot convert type 'int' to 'bool'
          >
          wtf, any ideas on how to work around this?
          >

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Cannot convert type 'int' to 'bool'

            John <no@spam.comwro te:
            bool isTrue = (test != 0);
            Yes, it seems that their is no implicit conversion of int to bool,
            therefore the only recourse is to specifically use an equivalence
            operator. Oh well, must have something to do with the overhead of the
            JIT. Well, not that the JIT implicitly has overhead, it's just that we
            have to specifically spoon feed it exactly we want.
            No, it's got nothing to do with JIT overhead - it's to do with program
            correctness. An implicit conversion from int to bool could easily lead
            to unintended consequences, such as:

            int x;
            if (x=3)
            {
            ....
            }

            Conceptually an integer isn't true or false. Just because C and C++
            have traditionally treated any non-zero value as "true" doesn't mean
            it's a good idea.

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • =?ISO-8859-1?Q?G=F6ran_Andersson?=

              #7
              Re: Cannot convert type 'int' to 'bool'

              Registered User wrote:
              On Fri, 21 Sep 2007 21:39:26 -0700, John <no@spam.comwro te:
              >
              >The following code:
              >>
              >int test = 1;
              >bool isTrue = (bool)test;
              >>
              >results in a compiler error:
              >>
              >Cannot convert type 'int' to 'bool'
              >>
              >wtf, any ideas on how to work around this?
              >
              bool isTrue = Convert.ToBoole an(test);
              >
              You will find that non-zero integer arguments to Convert.ToBoole an all
              return true.
              >
              regards
              A.G.
              The Convert.ToBoole an(int) method is implemented as:

              public static bool ToBoolean(int value) {
              return (value != 0);
              }

              So it's the same as doing the operation yourself:

              bool isTrue = (test != 0);

              Perhaps the compiler manages to inline the method call, so that the
              generated code is the same in both cases. Either way the difference in
              performance is normally negligable, so you should just go with the one
              that best describes why you are doing the conversion.

              :)

              --
              Göran Andersson
              _____
              Göran Anderssons privata hemsida.

              Comment

              Working...