473,395 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Nov 13 '05 #1
7 2090
"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
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" <pl****@ask.ie> 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
CDB
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" <kn*****@hotmail.com> 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" <pl****@ask.ie> 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
"CDB" <al***@delete.wave.co.nz> 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" <pl****@ask.ie> 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
"CDB" <al***@delete.wave.co.nz> 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
"Gerry Abbott" <pl****@ask.ie> 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
CDB,
No, actually. Premature click send on my part.

"CDB" <al***@delete.wave.co.nz> 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" <kn*****@hotmail.com> 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" <pl****@ask.ie> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
21
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the...
9
by: Earl | last post by:
I have somewhat of an interesting scenario: The form allows the user to select a service, which populates a a grid of product information related to that service ("service grid"). The user can...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
18
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.