Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

How to translate OrElse and AndAlso from VB.NET?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Siegfried Heintze - 11 Jan 2008 17:58 GMT
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
cfps.Christian - 11 Jan 2008 18:05 GMT
OrElse = ||
AndAlso = &&
Nicholas Paldino [.NET/C# MVP] - 11 Jan 2008 18:11 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Are there operators in C# that are the counterparts of "OrElse" and
> "AndAlso" that I can use when translating the following program from
[quoted text clipped - 21 lines]
>  End Module
> End Namespace
David Anton - 11 Jan 2008 23:08 GMT
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.
Signature

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

> Siegfried,
>
[quoted text clipped - 47 lines]
> >  End Module
> > End Namespace

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.