Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C# representation of VB.net "OrElse" operator?

81 views
Skip to first unread message

Rich P

unread,
Jun 23, 2009, 12:02:24 PM6/23/09
to

VB.net:
If this Or that Then...

C#
if (this || that){...}

VB.net If this OrElse that Then

C#
if (this .....?

I believe in VB.Net OrElse wont evaluate the next expression unless the
first expression is completely false where with OR both expressions get
evaluated - I think - I never get this part straight. Does C# have an
equivalent to OrElse? Or is it just a straight forward || for
everything that is Or/OrElse related?


Rich

*** Sent via Developersdex http://www.developersdex.com ***

Jeff Johnson

unread,
Jun 23, 2009, 12:16:30 PM6/23/09
to

"Rich P" <rpn...@aol.com> wrote in message
news:eFchAwB9...@TK2MSFTNGP02.phx.gbl...

> VB.net:
> If this Or that Then...
>
> C#
> if (this || that){...}
>
> VB.net If this OrElse that Then
>
> C#
> if (this .....?
>
> I believe in VB.Net OrElse wont evaluate the next expression unless the
> first expression is completely false where with OR both expressions get
> evaluated - I think - I never get this part straight. Does C# have an
> equivalent to OrElse? Or is it just a straight forward || for
> everything that is Or/OrElse related?

|| = OrElse. C# has NO equivalent to VB's OR statement as far as logical
comparisons go. In other words, you can't make C# NOT short-circuit. To do
so would require multiple if statements.

Bitwise, VB's OR = C#'s |.


Heandel

unread,
Jun 23, 2009, 12:18:22 PM6/23/09
to

Simple:

if(this)
{

}
else if (that)
{

}
else
{

}

Heandel

"Rich P" <rpn...@aol.com> a �crit dans le message de groupe de discussion :
eFchAwB9...@TK2MSFTNGP02.phx.gbl...

Jeff Johnson

unread,
Jun 23, 2009, 12:31:15 PM6/23/09
to

"Jeff Johnson" <i....@enough.spam> wrote in message
news:OgNn43B9...@TK2MSFTNGP02.phx.gbl...

I should expound. Normally you ALWAYS want short-circuiting. The only time
you wouldn't would be if the conditional had side effects and you wanted to
be sure to trigger those side effects for all conditionals. I think most
folks in this group would say that it's a bad idea to put all that into an
if statement and would instead recommend that you execute the functions to
produce those side effects first and capture the results, thne make your
conditional test the results.

In other words, I can't imagine why anyone would try to duplicate VB's
logical OR behavior. It was dumb to begin with (and this is coming from a VB
guy!) and I was thrilled to see VB "grow up" and FINALLY get a
short-circuiting operator. The few times I write in VB.NET (2005+) I always
use OrElse.


keremskusmezer

unread,
Jun 23, 2009, 2:33:14 PM6/23/09
to

Yes, it was fun when i switched my code from C# to VB.Net First Time.

if ( someStringObject != null && someStringObject.Length > 0)

NullReferenceException bada :)
if someStringObject is nothing and someStringObject.Length = 0 then

end if

CY

unread,
Jun 23, 2009, 3:00:07 PM6/23/09
to
>
> if ( someStringObject != null && someStringObject.Length > 0)
>
> NullReferenceException bada :)
> if someStringObject is nothing and someStringObject.Length = 0 then
>
> end if- Dölj citerad text -
>
> - Visa citerad text -

You did that or the "helper", havent tried anything C# but VB6->VB.NET
and it went ... lets just say it stays in VB6 for a while... still
cant sleep after the first chock... ;)

//CY

Family Tree Mike

unread,
Jun 23, 2009, 5:23:12 PM6/23/09
to

"Rich P" <rpn...@aol.com> wrote in message
news:eFchAwB9...@TK2MSFTNGP02.phx.gbl...


One bar tests both sides of the conditional, while two bars skips out if the
first is true.

--
Mike

Jeff Johnson

unread,
Jun 23, 2009, 5:32:59 PM6/23/09
to

"Family Tree Mike" <FamilyT...@ThisOldHouse.com> wrote in message
news:D1DE9B39-5F84-426F...@microsoft.com...

> One bar tests both sides of the conditional, while two bars skips out if
> the first is true.

Hmmm, now that I think about it, that WILL work, but I guess since I see " |
" as purely a bitwise operator I didn't consider this route. But won't the
compiler throw a warning? Ultimately, you normally SHOULDN'T do this, even
if you CAN....


Family Tree Mike

unread,
Jun 23, 2009, 6:06:33 PM6/23/09
to

"Jeff Johnson" <i....@enough.spam> wrote in message
news:O0jevoE9...@TK2MSFTNGP04.phx.gbl...


No, they are specifically allowed in the language (one bar, I mean).
& and && are also both defined for conditionals, but that is another
question...

--
Mike

Göran Andersson

unread,
Jun 24, 2009, 4:24:54 AM6/24/09
to

That's not correct. The | operator does that.

It's not very well known, and rarely used. Although I have known about
it for quite some time, I never had any use for it until a few days ago...

> Bitwise, VB's OR = C#'s |.

Only for integer operands.

--
G�ran Andersson
_____
http://www.guffa.com

Jeff Johnson

unread,
Jun 24, 2009, 9:10:55 AM6/24/09
to

"G�ran Andersson" <gu...@guffa.com> wrote in message
news:O0OJCVK...@TK2MSFTNGP05.phx.gbl...

>> || = OrElse. C# has NO equivalent to VB's OR statement as far as logical
>> comparisons go. In other words, you can't make C# NOT short-circuit. To
>> do so would require multiple if statements.
>
> That's not correct. The | operator does that.
>
> It's not very well known, and rarely used. Although I have known about it
> for quite some time, I never had any use for it until a few days ago...

So then this is a case where C# differs from C, right? Because I'm pretty
sure a C compiler would complain (warning) if it thought you used | when you
meant ||.


Göran Andersson

unread,
Jun 24, 2009, 1:00:09 PM6/24/09
to

Yes, in C the | and & operators are only binary operators, they are not
defined for logical operands. That makes a bit of sense, it would be
rather a mess as C has implicit conversion between integers and boolean.

How C# distinguishes between types is actually more like Pascal (Delphi)
than C.

Ben Voigt [C++ MVP]

unread,
Jun 24, 2009, 5:50:49 PM6/24/09
to

Family Tree Mike wrote:
> "Jeff Johnson" <i....@enough.spam> wrote in message
> news:O0jevoE9...@TK2MSFTNGP04.phx.gbl...
>> "Family Tree Mike" <FamilyT...@ThisOldHouse.com> wrote in message
>> news:D1DE9B39-5F84-426F...@microsoft.com...
>>
>>> One bar tests both sides of the conditional, while two bars skips
>>> out if the first is true.
>>
>> Hmmm, now that I think about it, that WILL work, but I guess since I
>> see "
>>> " as purely a bitwise operator I didn't consider this route. But
>>> won't
>> the compiler throw a warning? Ultimately, you normally SHOULDN'T do
>> this, even if you CAN....
>>
>
>
> No, they are specifically allowed in the language (one bar, I mean).

Which is to say that the language defines "||" as nothing more than "the
short-circuiting version of |"


J.B. Moreno

unread,
Jun 25, 2009, 12:53:40 AM6/25/09
to
G�ran Andersson <gu...@guffa.com> wrote:

> Jeff Johnson wrote:
> > "G�ran Andersson" <gu...@guffa.com> wrote in message

-snip-


> >> That's not correct. The | operator does that.
> >>
> >> It's not very well known, and rarely used. Although I have known about it
> >> for quite some time, I never had any use for it until a few days ago...
> >
> > So then this is a case where C# differs from C, right? Because I'm pretty
> > sure a C compiler would complain (warning) if it thought you used | when
> > you meant ||.
> >
>
> Yes, in C the | and & operators are only binary operators, they are not
> defined for logical operands. That makes a bit of sense, it would be
> rather a mess as C has implicit conversion between integers and boolean.

Actually, no. In C the result of the bitwise operation is compared to
zero, and if not equal it's considered true.

(Well, it might give a warning depending upon the specific compiler and
it's settings but it'd work).

--
J.B. Moreno

0 new messages