Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two Equal signs in one expression question

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
AU
For some time I have been pondering how one particular line of some code works (contributed some time ago by our beloved guru strongm).
This is used to check if a computer is connected to your network (pinging) and works very well.
Code:
If strComputer > "" Then
        Set objWMIService = GetObject("winmgmts:" & 

"{impersonationLevel=impersonate}!\\.\root\cimv2")
        Set objPing = objWMIService.ExecQuery("Select * From Win32_PingStatus 

Where Address = '" & strComputer & "' and StatusCode=0")
        'pings computer
        For Each objStatus In objPing
            IsReachable = objStatus.StatusCode = 0
        Next
End If

(IsReachable = True if computer is connected)
The line in my question is "IsReachable = objStatus.StatusCode = 0" where you have 2 equals sign in the one statement.

So it seems the code line is equivalent to
If objStatus.StatusCode = 0 then
IsReachable = True
Else
IsReachable = False
End IF

Similarly
a=0=0: print a gives true
a=1=0: print a gives true

I would be grateful for an explanation on how the 2 equal signs work to create a boolean result to put me out of my misery!
 
a=0=0: print a gives true

In this case, VB will evaluate the (0=0) part, which results in TRUE, and then assigns this to the a variable.

a=1=0: print a gives true

In this case, VB will evaluate the (1=0) part, which results in TRUE, and then assigns this to the a variable.

Personally, I don't like to see code like this because it can be a little misleading. Don't get me wrong, I have similar code in my project, but when I use this, I usually put parenthesis around the right side expression.

Instead of:
[tt]IsReachable = objStatus.StatusCode = 0[/tt]

I would write it as:

Code:
IsReachable = [!]([/!]objStatus.StatusCode = 0[!])[/!]

It will compile the same, but for me, it's more readable.

Make sense?

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
The first equal sign is an assignment operator, the second is a test for equivalence. In a different language it might look like:
Code:
IsReachable = objStatus.StatusCode == 0
 

VB will evaluate the (1=0) part, which results in TRUE[/quote gmmastros]
Hmmm....
Code:
Dim bln As Boolean

bln = (1 = 0)

MsgBox bln
Message box shows FALSE....

Have fun.

---- Andy
 
>a=1=0: print a gives true

Shouldn't do

>Message box shows FALSE....

As it should do
 
Yes I made a typo error - should have been
Similarly
a=0=0: print a gives true
a=1=0: print a gives true

I can see that it is doing what you all say but my question was how?
Having 2 equals like that I would have though should produce a syntax error.
I wondered if it was some accidental thing or was it planned?
 
>was it planned?

Planned, ted. It's to do with the VB expression evaluator and the rules it follows
 
It is interesting to see that, not only two, VB can support many = operators in an expression, although you rarely find such applications.

See the examples.
[tt]
a = 0 = 1 = 2 = 3
MsgBox a 'gives False
a = 3 = 2 = 1 = 0
MsgBox a 'gives True
[/tt]
As both expression contain the same operator (=), VB starts to evaluate them from Left to Right.

First expression:
a = 0 = 1 = 2 = 3 = (((0 = 1) = 2) = 3) = ((0 = 2) = 3) = (0 = 3) = False

Second expression:
a = 3 = 2 = 1 = 0 = (((3 = 2) = 1) = 0) = ((0 = 1) = 0) = (0 = 0) = True

A practical example of such expression is checking for a leap year.
[tt]IsLeap = (Year Mod 400 = 0) Or ((Not Year Mod 100 = 0) And (Year Mod 4 = 0))[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top