if statemet problem

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

    #1

    if statemet problem

    The statement below is resulting in this error:

    C:\Documents and Settings\Teddy\ My Documents\Visua l Studio
    Projects\Calend ar\Calendar.cs( 181): Operator '&&' cannot
    be applied to operands of type 'int' and 'bool'

    int mmonth,m;
    if((mmonth = 1) && ((m > 1) && (m < 14)))

    I tried to change the second argument to a bool and
    received the same error.
    What is going on? Does someone have suggestions please?


  • José Joye

    #2
    Re: if statemet problem

    replace

    mmonth = 1 with mmonth == 1

    José
    "tz" <anonymous@disc ussions.microso ft.com> a écrit dans le message de
    news:068e01c3b1 04$360d9070$a50 1280a@phx.gbl.. .[color=blue]
    > The statement below is resulting in this error:
    >
    > C:\Documents and Settings\Teddy\ My Documents\Visua l Studio
    > Projects\Calend ar\Calendar.cs( 181): Operator '&&' cannot
    > be applied to operands of type 'int' and 'bool'
    >
    > int mmonth,m;
    > if((mmonth = 1) && ((m > 1) && (m < 14)))
    >
    > I tried to change the second argument to a bool and
    > received the same error.
    > What is going on? Does someone have suggestions please?
    >
    >[/color]


    Comment

    • Lynn Harrison

      #3
      Re: if statemet problem

      tz,

      a good programming practive to avoid this type of typo is to use

      if ( ( 1 == mmonth ) ...

      instead.

      This format will give a syntax error if you miss the second equals sign.


      --

      Lynn Harrison
      SHAMELESS PLUG - Introduction to 3D Game Engine Design (C# & DX9)



      "José Joye" <jose.joye@__No _SPam__bluewin_ _maPS_oN__.ch> wrote in message
      news:OxnUQYQsDH A.556@TK2MSFTNG P11.phx.gbl...[color=blue]
      > replace
      >
      > mmonth = 1 with mmonth == 1
      >
      > José
      > "tz" <anonymous@disc ussions.microso ft.com> a écrit dans le message de
      > news:068e01c3b1 04$360d9070$a50 1280a@phx.gbl.. .[color=green]
      > > The statement below is resulting in this error:
      > >
      > > C:\Documents and Settings\Teddy\ My Documents\Visua l Studio
      > > Projects\Calend ar\Calendar.cs( 181): Operator '&&' cannot
      > > be applied to operands of type 'int' and 'bool'
      > >
      > > int mmonth,m;
      > > if((mmonth = 1) && ((m > 1) && (m < 14)))
      > >
      > > I tried to change the second argument to a bool and
      > > received the same error.
      > > What is going on? Does someone have suggestions please?
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • tz

        #4
        Re: if statemet problem

        Beginner mistake.
        thanks.[color=blue]
        >-----Original Message-----
        >tz,
        >
        >a good programming practive to avoid this type of typo is [/color]
        to use[color=blue]
        >
        >if ( ( 1 == mmonth ) ...
        >
        >instead.
        >
        >This format will give a syntax error if you miss the [/color]
        second equals sign.[color=blue]
        >
        >
        >--
        >
        >Lynn Harrison
        >SHAMELESS PLUG - Introduction to 3D Game Engine Design [/color]
        (C# & DX9)[color=blue]
        >www.forums.Apress.com
        >
        >
        >"José Joye" <jose.joye@__No _SPam__bluewin_ _maPS_oN__.ch> [/color]
        wrote in message[color=blue]
        >news:OxnUQYQsD HA.556@TK2MSFTN GP11.phx.gbl...[color=green]
        >> replace
        >>
        >> mmonth = 1 with mmonth == 1
        >>
        >> José
        >> "tz" <anonymous@disc ussions.microso ft.com> a écrit dans [/color][/color]
        le message de[color=blue][color=green]
        >> news:068e01c3b1 04$360d9070$a50 1280a@phx.gbl.. .[color=darkred]
        >> > The statement below is resulting in this error:
        >> >
        >> > C:\Documents and Settings\Teddy\ My Documents\Visua l [/color][/color][/color]
        Studio[color=blue][color=green][color=darkred]
        >> > Projects\Calend ar\Calendar.cs( 181): Operator '&&' [/color][/color][/color]
        cannot[color=blue][color=green][color=darkred]
        >> > be applied to operands of type 'int' and 'bool'
        >> >
        >> > int mmonth,m;
        >> > if((mmonth = 1) && ((m > 1) && (m < 14)))
        >> >
        >> > I tried to change the second argument to a bool and
        >> > received the same error.
        >> > What is going on? Does someone have suggestions [/color][/color][/color]
        please?[color=blue][color=green][color=darkred]
        >> >
        >> >[/color]
        >>
        >>[/color]
        >
        >
        >.
        >[/color]

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: if statemet problem

          Lynn Harrison <lharrison@dvc4 00.com.NOSPAM> wrote:[color=blue]
          > a good programming practive to avoid this type of typo is to use
          >
          > if ( ( 1 == mmonth ) ...
          >
          > instead.[/color]

          Not in C#. Note that the OP was already getting a syntax error - it
          wasn't compiling and managing to assign 1 to mmonth.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Bruno Jouhier [MVP]

            #6
            Re: if statemet problem


            "Lynn Harrison" <lharrison@dvc4 00.com.NOSPAM> a écrit dans le message de
            news:e3Ho62QsDH A.2252@TK2MSFTN GP09.phx.gbl...[color=blue]
            > tz,
            >
            > a good programming practive to avoid this type of typo is to use
            >
            > if ( ( 1 == mmonth ) ...[/color]

            No, this is the kind of nonsense that you have to write in C and C++ because
            the typing rules are too weak (lack of boolean type).

            But you don't need to write it this way in C# because the C# compiler will
            give you an error if you write if (mmonth = 1) instead of if (mmonth == 1).
            So, the recommended way to write it in C# is: if (mmonth == 1)

            Bruno.

            [color=blue]
            >
            > instead.
            >
            > This format will give a syntax error if you miss the second equals sign.
            >
            >
            > --
            >
            > Lynn Harrison
            > SHAMELESS PLUG - Introduction to 3D Game Engine Design (C# & DX9)
            > www.forums.Apress.com
            >
            >
            > "José Joye" <jose.joye@__No _SPam__bluewin_ _maPS_oN__.ch> wrote in message
            > news:OxnUQYQsDH A.556@TK2MSFTNG P11.phx.gbl...[color=green]
            > > replace
            > >
            > > mmonth = 1 with mmonth == 1
            > >
            > > José
            > > "tz" <anonymous@disc ussions.microso ft.com> a écrit dans le message de
            > > news:068e01c3b1 04$360d9070$a50 1280a@phx.gbl.. .[color=darkred]
            > > > The statement below is resulting in this error:
            > > >
            > > > C:\Documents and Settings\Teddy\ My Documents\Visua l Studio
            > > > Projects\Calend ar\Calendar.cs( 181): Operator '&&' cannot
            > > > be applied to operands of type 'int' and 'bool'
            > > >
            > > > int mmonth,m;
            > > > if((mmonth = 1) && ((m > 1) && (m < 14)))
            > > >
            > > > I tried to change the second argument to a bool and
            > > > received the same error.
            > > > What is going on? Does someone have suggestions please?
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            Working...