473,386 Members | 1,798 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,386 software developers and data experts.

How to translate OrElse and AndAlso from VB.NET?

Are there operators in C# that are the counterparts of "OrElse" and
"AndAlso" that I can use when translating the following program from VB.NET
to C#?

Thanks,
Siegfried

Namespace vbtest
Module Main
Function reflect(x as boolean) as boolean
System.Console.Out.WriteLine(" reflect = {0}", x)
reflect = x
End Function
Sub Main(ByVal args() As String)
System.Console.Out.WriteLine(" reflect(true) Or reflect(true) =
{0}", reflect(true) Or reflect(true))
System.Console.Out.WriteLine(" reflect(true) OrElse reflect(true) =
{0}", reflect(true) OrElse reflect(true))
System.Console.Out.WriteLine(" reflect(false) And reflect(false) =
{0}", reflect(false) And reflect(false))
System.Console.Out.WriteLine(" reflect(false) AndAlso reflect(false)
= {0}", reflect(false) AndAlso reflect(false))
End Sub
End Module
End Namespace
Jan 11 '08 #1
3 19207
OrElse = ||
AndAlso = &&
Jan 11 '08 #2
Siegfried,

Yes, && and || respectively.

C#, Java and other C++-based languages have a tradition of
short-circuiting expressions if the outcome can be determined with the first
expression.

For example, if you had a method that returned true, and another that
returned false, and did this:

if (ReturnsTrue() || ReturnsFalse())

In C#, Java, etc, etc, the ReturnsFalse method would never be called, as
the ReturnsTrue method returns true, and anything ORed with true is true, so
there is no need to evaluate the second expression. The same goes for:

if (ReturnsFalse() && ReturnsTrue())

Since false ANDed with anything is false, it doesn't execute
ReturnsTrue.

Now, in VB, all the expressions are ALWAYS evaluated (when using And or
Or), which is why AndAlso and OrElse were added, so that you could have this
short-circuiting.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Siegfried Heintze" <si*******@heintze.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Are there operators in C# that are the counterparts of "OrElse" and
"AndAlso" that I can use when translating the following program from
VB.NET to C#?

Thanks,
Siegfried

Namespace vbtest
Module Main
Function reflect(x as boolean) as boolean
System.Console.Out.WriteLine(" reflect = {0}", x)
reflect = x
End Function
Sub Main(ByVal args() As String)
System.Console.Out.WriteLine(" reflect(true) Or reflect(true) =
{0}", reflect(true) Or reflect(true))
System.Console.Out.WriteLine(" reflect(true) OrElse reflect(true) =
{0}", reflect(true) OrElse reflect(true))
System.Console.Out.WriteLine(" reflect(false) And reflect(false) =
{0}", reflect(false) And reflect(false))
System.Console.Out.WriteLine(" reflect(false) AndAlso
reflect(false) = {0}", reflect(false) AndAlso reflect(false))
End Sub
End Module
End Namespace


Jan 11 '08 #3
Actually, there is no difference between VB and C# in this regard.
Both have short-circuiting logical operators (AndAlso/OrElse and &&/||).

Both have non-short-circuiting logical operators, which also serve as
bitwise operators:
And/Or in VB
& and | in C#

The proper use of And/Or and &/| is for bitwise operations, but they can
also be used for (inefficient) logical operations.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++ VB Edition: VB to C++/CLI
Instant C++ C# Edition: C# to C++/CLI
"Nicholas Paldino [.NET/C# MVP]" wrote:
Siegfried,

Yes, && and || respectively.

C#, Java and other C++-based languages have a tradition of
short-circuiting expressions if the outcome can be determined with the first
expression.

For example, if you had a method that returned true, and another that
returned false, and did this:

if (ReturnsTrue() || ReturnsFalse())

In C#, Java, etc, etc, the ReturnsFalse method would never be called, as
the ReturnsTrue method returns true, and anything ORed with true is true, so
there is no need to evaluate the second expression. The same goes for:

if (ReturnsFalse() && ReturnsTrue())

Since false ANDed with anything is false, it doesn't execute
ReturnsTrue.

Now, in VB, all the expressions are ALWAYS evaluated (when using And or
Or), which is why AndAlso and OrElse were added, so that you could have this
short-circuiting.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Siegfried Heintze" <si*******@heintze.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Are there operators in C# that are the counterparts of "OrElse" and
"AndAlso" that I can use when translating the following program from
VB.NET to C#?

Thanks,
Siegfried

Namespace vbtest
Module Main
Function reflect(x as boolean) as boolean
System.Console.Out.WriteLine(" reflect = {0}", x)
reflect = x
End Function
Sub Main(ByVal args() As String)
System.Console.Out.WriteLine(" reflect(true) Or reflect(true) =
{0}", reflect(true) Or reflect(true))
System.Console.Out.WriteLine(" reflect(true) OrElse reflect(true) =
{0}", reflect(true) OrElse reflect(true))
System.Console.Out.WriteLine(" reflect(false) And reflect(false) =
{0}", reflect(false) And reflect(false))
System.Console.Out.WriteLine(" reflect(false) AndAlso
reflect(false) = {0}", reflect(false) AndAlso reflect(false))
End Sub
End Module
End Namespace


Jan 11 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Mike Hale | last post by:
Is it better to use AndAlso and OrElse by default rather than the regual And & Or? MikeH
16
by: Raterus | last post by:
I kinda just stumbled across these operators, they seem great, as you can forget the second expression depending on the result of the first, but are there any cons to using these? From my...
11
by: A Traveler | last post by:
I was just curious if anyone knows how the combinations of And/AndAlso and Or/OrElse compare in terms of performance. Which takes more work for the system, performing two evaluations on an And or...
9
by: Lior | last post by:
Hello . I know that the AndAlso and OrElse statements are short-circuiting And and Or statements , respectively . Should I always use (I don't like the word "always" ...) AndAlso instead of...
30
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, If x = y And m = n The .... End If If x = y AndAlso m = n Then .... End If
12
by: Al G | last post by:
Ok, so I've started to use it, and it is quicker. Now, why is it that "AND" doesn't already work this way? Al G
8
by: Euvin | last post by:
I am kind of confuse as to how these operators work: AndAlso and OrElse Anyone care to explain. Some examples would be great!
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.