Link to home
Start Free TrialLog in
Avatar of g_johnson
g_johnsonFlag for United States of America

asked on

I have a VB.Net assignment statement with an or clause. What does it mean?

VB.Net

dim a as Boolean = false
dim b as Boolean = true
dim c as Boolean

This line of code confuses me:
c = a or b

I've never seen a line of code like that.  What does it mean?

My best guess is that it means if a is null then set c to equal b, but that's truly a wild guess.  Is this even a valid line of code?
SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of g_johnson

ASKER

So, I think you are saying that if either a or b is true, c will be true.  Is that correct?

The only way c is false is if both a and b are false.  Still correct?


Thanks!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>So, I think you are saying that if either a or b is true, c will be true.  Is that correct?

correct

>>The only way c is false is if both a and b are false.  Still correct?

False. If a OR b is false, c is set to false.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This all stems from a bitwise operation whereas 0 = False and 1 = True.  So changing our program we can  prove at the bit level that the boolean operation is correct:
Module Module1
	Sub Main()
		Console.WriteLine("Let's test Or")
		OrTest(1, 1)
		OrTest(0, 1)
		OrTest(1, 0)
		OrTest(0, 0)
		Console.WriteLine()
		Console.WriteLine("Now let's test And")
		AndTest(1, 1)
		AndTest(0, 1)
		AndTest(1, 0)
		AndTest(0, 0)
		Console.ReadLine()
	End Sub

	Sub OrTest(ByVal lhs As Short, ByVal rhs As Short)
		Console.WriteLine("{0} OR {1} = {2}", lhs, rhs, lhs Or rhs)
	End Sub

	Sub AndTest(ByVal lhs As Short, ByVal rhs As Short)
		Console.WriteLine("{0} AND {1} = {2}", lhs, rhs, lhs And rhs)
	End Sub
End Module

Open in new window

Which now produces the following output -User generated image-saige-
Thanks, all!