何时使用AndAlso vs And? [英] When to use AndAlso vs And ?

查看:57
本文介绍了何时使用AndAlso vs And?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,


如果x = y且m = n

....

结束如果


如果x = y AndAlso m = n那么

....

结束如果


我什么时候需要使用AndAlso vs And?


谢谢,

Rich

Greetings,

If x = y And m = n The
....
End If

If x = y AndAlso m = n Then
....
End If

When do I need to use AndAlso vs And?

Thanks,
Rich

推荐答案

对于您提供的示例,您正在进行严格的逻辑操作。使用

AndAlso因为它更有效率(如果第一个是假的,它不会评估第二个

操作数。)

和运算符通常仅用于按位运算。你可以

也使用和对于逻辑运算,但它总是评估两个操作数。

-

David Anton
www.tangiblesoftwaresolutions.com

即时C#:VB到C#转换器

即时VB:C#到VB转换器

即时C ++:C#/ VB到C ++转换器

即时Python:VB到Python转换器

" Rich"写道:
For the sample you provided, you are doing a strictly logical operation. Use
the "AndAlso" since it''s more efficient (it doesn''t evaluate the second
operand if the first is false).
The "And" operator is usually just used for bitwise operations. You can
also use "And" for logical operations, but it evaluates both operands always.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"Rich" wrote:

问候,


如果x = y且m = n

。 ..

结束如果


如果x = y并且还要m = n那么

...

结束如果


我什么时候需要使用AndAlso vs And?


谢谢,

Rich
Greetings,

If x = y And m = n The
...
End If

If x = y AndAlso m = n Then
...
End If

When do I need to use AndAlso vs And?

Thanks,
Rich


如果您使用And,那么它会处理所有条款然后决定

如果它是真的或者不是。

所以它检查(x = y)和(m = n)。


如果你使用AndAlso,它会停止处理当它遇到

为假的情况时。因此,如果(x = y)

为假,则它永远不会检查(m = n)。这样效率更高,因为它没有b
$ b不必要的处理,也可以很方便,如果你想做什么

的东西像这样:


If(rdr IsNot Nothing)AndAlso rdr.Rows(0).Items(0).ToString =" abc" ...


与VB6不同,如果rdr什么都没有,它就不会被废弃,因为它不会对b
进行处理

第二条款。


对于或运营商,他们添加了OrElse它做了同样的事情。

它被称为短路。


我相信这就是和的方式。和或在C / C ++ / C#中工作。我认为他们



刚刚改变了和和或在VB中,但它会吓坏了很多人b / b
人们从VB6转换时需要更多的工作,所以他们只需添加
新结构。


Robin S.

(比你想要的更多信息,不是吗?)

------------------------------


" Rich" ; < Ri ** @ discussion.microsoft.com写信息

news:82 ************************** ******** @ microsof t.com ...
If you use "And", then it processes all of the clauses and then decides
if it''s true or not.
So it checks both (x=y) and (m=n).

If you use "AndAlso", it stops processing when it hits a condition that
is false. So if (x=y)
is false, it never checks (m=n). This is more efficient because it
doesn''t do
unnecessary processing, and also can be handy, if you want to do
something like this:

If (rdr IsNot Nothing) AndAlso rdr.Rows(0).Items(0).ToString = "abc" ...

Unlike VB6, it won''t crap out if rdr is nothing, because it won''t
process the
second clause.

For the "or" operator, they added "OrElse" which does the same thing.
It''s called short-circuiting.

I believe this is the way "and" and "or" work in C/C++/C#. I think they
would
have just changed "and" and "or" in VB, but it would have freaked out
too many
people and required more work when converting from VB6, so they just
added
new constructs.

Robin S.
(more info than you wanted, wasn''t it?)
------------------------------

"Rich" <Ri**@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...

问候,


如果x = y并且m = n

...

结束如果


如果x = y AndAlso m = n那么

...

结束如果


我什么时候需要使用AndAlso vs And?


谢谢,

Rich
Greetings,

If x = y And m = n The
...
End If

If x = y AndAlso m = n Then
...
End If

When do I need to use AndAlso vs And?

Thanks,
Rich



它在VB6中运行得足够多的垃圾


不必要的变化并不好。

这是不必要的


-Aaron

RobinS写道:
what a load of crap it worked well enough in VB6

unnecessary change isn''t good.
it''s unnecessary

-Aaron
RobinS wrote:

如果你使用And,那么它处理所有条款然后决定

如果它'这是真的与否。

所以它检查(x = y)和(m = n)。


如果你使用AndAlso,它会在达到
为fa的条件时停止处理伦敦政治经济学院。因此,如果(x = y)

为假,则它永远不会检查(m = n)。这样效率更高,因为它没有b
$ b不必要的处理,也可以很方便,如果你想做什么

的东西像这样:


If(rdr IsNot Nothing)AndAlso rdr.Rows(0).Items(0).ToString =" abc" ...


与VB6不同,如果rdr什么都没有,它就不会被废弃,因为它不会对b
进行处理

第二条款。


对于或运营商,他们添加了OrElse它做了同样的事情。

它被称为短路。


我相信这就是和的方式。和或在C / C ++ / C#中工作。我认为他们



刚刚改变了和和或在VB中,但它会吓坏了很多人b / b
人们从VB6转换时需要更多的工作,所以他们只需添加
新结构。


Robin S.

(比你想要的更多信息,不是吗?)

------------------------------


" Rich" ; < Ri ** @ discussion.microsoft.com写信息

news:82 ************************** ******** @ microsof t.com ...
If you use "And", then it processes all of the clauses and then decides
if it''s true or not.
So it checks both (x=y) and (m=n).

If you use "AndAlso", it stops processing when it hits a condition that
is false. So if (x=y)
is false, it never checks (m=n). This is more efficient because it
doesn''t do
unnecessary processing, and also can be handy, if you want to do
something like this:

If (rdr IsNot Nothing) AndAlso rdr.Rows(0).Items(0).ToString = "abc" ...

Unlike VB6, it won''t crap out if rdr is nothing, because it won''t
process the
second clause.

For the "or" operator, they added "OrElse" which does the same thing.
It''s called short-circuiting.

I believe this is the way "and" and "or" work in C/C++/C#. I think they
would
have just changed "and" and "or" in VB, but it would have freaked out
too many
people and required more work when converting from VB6, so they just
added
new constructs.

Robin S.
(more info than you wanted, wasn''t it?)
------------------------------

"Rich" <Ri**@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...

问候,


如果x = y并且m = n

...

结束如果


如果x = y AndAlso m = n那么

...

结束如果


我什么时候需要使用AndAlso vs And?


谢谢,

Rich
Greetings,

If x = y And m = n The
...
End If

If x = y AndAlso m = n Then
...
End If

When do I need to use AndAlso vs And?

Thanks,
Rich


这篇关于何时使用AndAlso vs And?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆