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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OrElse doesn't seem to work.

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
I am trying to find out why OrElse doesn't work in my code which is a snippet of code I put together to test the OrElse.

If I take the following code:

Module Module1

Sub Main()

Dim str As String = String.Empty
Dim j As Integer
Dim k As Integer

str = "aaa"
j = 5
k = 0

If str = String.Empty Or (j / k) Then
Console.WriteLine("Inside If statement")
Else
Console.WriteLine("Inside Else Statement")
End If
Console.ReadLine()
End Sub
End Module

I get a divide by zero error (which was expected). But if I change the Or to OrElse, I don't get the error which I figured I would.

Module Module1

Sub Main()

Dim str As String = String.Empty
Dim j As Integer
Dim k As Integer

str = "aaa"
j = 5
k = 0

If str = String.Empty OrElse (j / k) Then
Console.WriteLine("Inside If statement")
Else
Console.WriteLine("Inside Else Statement")
End If
Console.ReadLine()
End Sub
End Module


Since "str = String.Empty" is false, the next test should go to "(j / k)" which should get an error (as it did before), but prints the "Inside If statement".

Why is that?

I know the OrElse will check the 1st test and if it fails (as it does here) it goes to the next.

What am I missing?

Thanks,

Tom
 


Not sure it's evaluating the way you think it is with OrElse

Replace your Console.WriteLine() inside the if-then-else with Console.WriteLine(j / k) and see what is output...


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
That was interesting.

It evaluated it as true and the new line showed me infinity.

When actually I would think it should have given me a divide by zero error as the "Or" did it either in the OrElse clause or the Console.WriteLine statement.

Seems like an error.

Thanks,

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top