An expression question

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

    #1

    An expression question

    Probly belongs to a programmers group, but I want to evaluate the expression

    If a=b=c=d=e then (if there are all equal)
    something
    else
    something else.

    Is there an easy way for this in VB?


    Gerry Abbott





  • Larry  Linson

    #2
    Re: An expression question

    "Gerry Abbott" wrote
    [color=blue]
    > If a=b=c=d=e then (if there are all equal)
    > something
    > else
    > something else.
    >
    > Is there an easy way for this in VB?[/color]

    Alas, not as easy as one might hope:

    If a=b And b=c And c=d And d=e Then

    Because an equal sign in an expression may be evaluated as a logical
    operator, you have to be careful. For example:

    Dim boolB as Boolean
    Dim intA as Integer
    Dim intB as Integer

    boolB = intA = intB

    is a perfectly valid statement. If intA and intB are equal, boolB will be
    set to the value of True; if not, boolB will be set to the value False.

    Larry Linson
    Microsoft Access MVP





    Comment

    • Alan Webb

      #3
      Re: An expression question

      Gerry,
      Assuming a, b, c, d, & e are all variables, then what you wrote will work
      fine. A more typical way to write it would be:

      If A and B and C and D and E Then
      'Do what needs doing
      Else
      'Do the other thing
      End if


      "Gerry Abbott" <please@ask.i e> wrote in message
      news:x1lGc.3967 $Z14.4998@news. indigo.ie...[color=blue]
      > Probly belongs to a programmers group, but I want to evaluate the[/color]
      expression[color=blue]
      >
      > If a=b=c=d=e then (if there are all equal)
      > something
      > else
      > something else.
      >
      > Is there an easy way for this in VB?
      >
      >
      > Gerry Abbott
      >
      >
      >
      >
      >[/color]


      Comment

      • CDB

        #4
        Re: An expression question

        Are you sure, Alan?
        If the variables held boolean values, ok, but:

        ?237 and 237 and 237
        237

        ?237 and 237 and 236
        236
        Unless A, B or C are zero, A And B And C will always evaluate to not-zero,
        or True. Unhelpful.

        ?237 = 237 = 237
        False
        A true would be yielded only in a few of the cases where the values were Not
        equal.

        If the data type is string, then a type mismatch error occurs.

        Clive


        "Alan Webb" <knogeek@hotmai l.com> wrote in message
        news:cGoGc.99$C D4.48075@news.u swest.net...[color=blue]
        > Gerry,
        > Assuming a, b, c, d, & e are all variables, then what you wrote will work
        > fine. A more typical way to write it would be:
        >
        > If A and B and C and D and E Then
        > 'Do what needs doing
        > Else
        > 'Do the other thing
        > End if
        >
        >
        > "Gerry Abbott" <please@ask.i e> wrote in message
        > news:x1lGc.3967 $Z14.4998@news. indigo.ie...[color=green]
        > > Probly belongs to a programmers group, but I want to evaluate the[/color]
        > expression[color=green]
        > >
        > > If a=b=c=d=e then (if there are all equal)
        > > something
        > > else
        > > something else.
        > >
        > > Is there an easy way for this in VB?
        > >
        > >
        > > Gerry Abbott
        > >
        > >
        > >
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Michael \(michka\) Kaplan [MS]

          #5
          Re: An expression question

          "CDB" <alpha@delete.w ave.co.nz> wrote...
          [color=blue]
          > ?237 and 237 and 237
          > 237[/color]

          This is expected -- bitmasking works that way....
          [color=blue]
          > ?237 and 237 and 236
          > 236[/color]

          Well, &hED and &hEC (the hex forms of the two numbers) sdhare all bytes
          except that one -- so this statement, which masks the one byte, will return
          exactly that.
          [color=blue]
          > Unless A, B or C are zero, A And B And C will always evaluate
          > to not-zero, or True. Unhelpful.[/color]

          Depends on what you are trying to check....
          [color=blue]
          > ?237 = 237 = 237
          > False
          > A true would be yielded only in a few of the cases where the values
          > were Not equal.[/color]

          Instead try:

          ? 237 = 237 = True
          True

          and then you will see what is going on. It evalusted your expression as:

          (237 = 237) = 237

          The first part is True, and thus you get

          True = 237

          which is of course false.
          [color=blue]
          > If the data type is string, then a type mismatch error occurs.[/color]

          Well, not always (due to VBA evil type conversion):

          ? "True" = true
          True
          ? "237" = 237
          True

          Looking at Gerry's original question:
          [color=blue][color=green]
          > > "Gerry Abbott" <please@ask.i e> wrote in message
          > > news:x1lGc.3967 $Z14.4998@news. indigo.ie...[color=darkred]
          > > > Probly belongs to a programmers group, but I want to evaluate the[/color]
          > > expression[color=darkred]
          > > >
          > > > If a=b=c=d=e then (if there are all equal)
          > > > something
          > > > else
          > > > something else.
          > > >
          > > > Is there an easy way for this in VB?[/color][/color][/color]

          This will fail, since it will be evaluated as:

          If ( ( ( a = b ) = c ) = d ) = e

          which will never "work" as intended. Instead try:

          If A=B And B=C And C=D and D=E Then

          and this will work.


          --
          MichKa [MS]
          NLS Collation/Locale/Keyboard Development
          Globalization Infrastructure and Font Technologies
          Windows International Division

          This posting is provided "AS IS" with
          no warranties, and confers no rights.


          Comment

          • Lyle Fairfield

            #6
            Re: An expression question

            "CDB" <alpha@delete.w ave.co.nz> wrote in
            news:ccgn46$bp0 $1@news.wave.co .nz:
            [color=blue]
            > Are you sure, Alan?
            > If the variables held boolean values, ok, but:
            >
            > ?237 and 237 and 237
            > 237
            >
            > ?237 and 237 and 236
            > 236
            > Unless A, B or C are zero, A And B And C will always evaluate to
            > not-zero, or True. Unhelpful.[/color]

            I don't think this is so.

            If A, B, and C have no common bits set, as in 1, 2, 4
            (00000001,00000 010,00000100), A And B And C will evaulate to zero (False).

            I do not think And, of itself, can be used satisfactorily to test equality.
            If A, B, and C have a common bit set, as in 2, 6, 10
            (00000010,00000 110,00001010), A And B And C will evaulate to two -> non-zero
            (True).

            An untested air-code method, (but I think it's satisfactory) to test several
            whole numbers for equality could be:
            CBool((A Or B Or C Or ...) = A)

            --
            Lyle
            --
            use iso date format: yyyy-mm-dd
            http://www.w3.org/QA/Tips/iso-date
            --
            The e-mail address isn't, but you could use it to find one.

            Comment

            • David W. Fenton

              #7
              Re: An expression question

              "Gerry Abbott" <please@ask.i e> wrote in
              news:x1lGc.3967 $Z14.4998@news. indigo.ie:
              [color=blue]
              > Probly belongs to a programmers group, but I want to evaluate the
              > expression
              >
              > If a=b=c=d=e then (if there are all equal)
              > something
              > else
              > something else.
              >
              > Is there an easy way for this in VB?[/color]

              If (a+b+c+d+e)/5 = a Then

              That will work reliably only for integers, though, unless you
              control the rounding.

              You might try:

              If a-b+c-d+e = a Then

              which would get rid of the floating-point inaccuracies introduced by
              division (though there can be floating-point inaccuracies in any
              operation).

              Of course, what you really want is:

              If (b=a) And (c=a) And (d=a) And (e=a) Then

              With that, you don't have to worry about floating-point errors at
              all.

              --
              David W. Fenton http://www.bway.net/~dfenton
              dfenton at bway dot net http://www.bway.net/~dfassoc

              Comment

              • Alan Webb

                #8
                Re: An expression question

                CDB,
                No, actually. Premature click send on my part.

                "CDB" <alpha@delete.w ave.co.nz> wrote in message
                news:ccgn46$bp0 $1@news.wave.co .nz...[color=blue]
                > Are you sure, Alan?
                > If the variables held boolean values, ok, but:
                >
                > ?237 and 237 and 237
                > 237
                >
                > ?237 and 237 and 236
                > 236
                > Unless A, B or C are zero, A And B And C will always evaluate to not-zero,
                > or True. Unhelpful.
                >
                > ?237 = 237 = 237
                > False
                > A true would be yielded only in a few of the cases where the values were[/color]
                Not[color=blue]
                > equal.
                >
                > If the data type is string, then a type mismatch error occurs.
                >
                > Clive
                >
                >
                > "Alan Webb" <knogeek@hotmai l.com> wrote in message
                > news:cGoGc.99$C D4.48075@news.u swest.net...[color=green]
                > > Gerry,
                > > Assuming a, b, c, d, & e are all variables, then what you wrote will[/color][/color]
                work[color=blue][color=green]
                > > fine. A more typical way to write it would be:
                > >
                > > If A and B and C and D and E Then
                > > 'Do what needs doing
                > > Else
                > > 'Do the other thing
                > > End if
                > >
                > >
                > > "Gerry Abbott" <please@ask.i e> wrote in message
                > > news:x1lGc.3967 $Z14.4998@news. indigo.ie...[color=darkred]
                > > > Probly belongs to a programmers group, but I want to evaluate the[/color]
                > > expression[color=darkred]
                > > >
                > > > If a=b=c=d=e then (if there are all equal)
                > > > something
                > > > else
                > > > something else.
                > > >
                > > > Is there an easy way for this in VB?
                > > >
                > > >
                > > > Gerry Abbott
                > > >
                > > >
                > > >
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >
                >[/color]


                Comment

                Working...