459,213 Members | 1,296 Online Need help? Post your question and get tips & solutions from a community of 459,213 IT Pros & Developers. It's quick & easy.

# An expression question

 P: n/a 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 Nov 13 '05 #1
7 Replies

 P: n/a "Gerry Abbott" wrote 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? 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 Nov 13 '05 #2

 P: n/a 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" wrote in message news:x1*****************@news.indigo.ie... 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 Nov 13 '05 #3

 P: n/a 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" wrote in message news:cG****************@news.uswest.net... 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" wrote in message news:x1*****************@news.indigo.ie... 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 Nov 13 '05 #4

 P: n/a "CDB" wrote... ?237 and 237 and 237 237 This is expected -- bitmasking works that way.... ?237 and 237 and 236 236 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. Unless A, B or C are zero, A And B And C will always evaluate to not-zero, or True. Unhelpful. Depends on what you are trying to check.... ?237 = 237 = 237 False A true would be yielded only in a few of the cases where the values were Not equal. 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. If the data type is string, then a type mismatch error occurs. Well, not always (due to VBA evil type conversion): ? "True" = true True ? "237" = 237 True Looking at Gerry's original question: "Gerry Abbott" wrote in message news:x1*****************@news.indigo.ie... 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? 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. Nov 13 '05 #5

 P: n/a "CDB" wrote in news:cc**********@news.wave.co.nz: 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. I don't think this is so. If A, B, and C have no common bits set, as in 1, 2, 4 (00000001,00000010,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,00000110,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. Nov 13 '05 #6

 P: n/a "Gerry Abbott" wrote in news:x1*****************@news.indigo.ie: 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? 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 Nov 13 '05 #7

 P: n/a CDB, No, actually. Premature click send on my part. "CDB" wrote in message news:cc**********@news.wave.co.nz... 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" wrote in message news:cG****************@news.uswest.net... 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" wrote in message news:x1*****************@news.indigo.ie... 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 Nov 13 '05 #8

### This discussion thread is closed

Replies have been disabled for this discussion. 