博客网 >

VB.NET operator "andalso" "orelse"
作者:分类:默认分类标签:

比较有用的关键词,虽然VB.NET依然支持or,and, 建议以后都改为andalso, orelse这样可以使程序更加健壮,或者直接使用C#,可以避免这些问题。

VB .NET features two new logical operators that help make your programming ... well ... more logical. The new operators are AndAlso and OrElse and they generally replace the VB 6 operators And and Or (but they do much more!).

The old VB 6 And and Or are still present in VB .NET. If you decide that they're what you need your program, go ahead and use them. They work the same way they used to work.

But AndAlso and OrElse have some properties that enhance your code in ways that VB 6 can't match. They offer advantages in two general categories:

  • You can avoid executing part of a logical expression to avoid problems.
  • You can optimize code by not executing any more of a compound expression than required.

AndAlso and OrElse are pretty much like And and Or except that they will "short circuit" an expression once the outcome is guaranteed. Suppose you're coding a test of a calculation result like this:

If a < Value1 And b < (Value2 / Value3) Then
' {body of If block}
End If

If the variables in the block have these values:

Value1 = 1
Value2 = 1
Value3 = 0
a = 1
b = 1

The If expression generates a "divide by zero" error in VB 6 because Value3 is zero. It could be that the cases that result in Value3 being zero are very rare and only occur when you're enjoying a vacation a thousand miles away so you can be called back to fix the program in an emergency mode. (Hey! It happens!)

Let's recode the program as a .NET program using AndAlso and see what happens.

Dim Value1 As Integer
Dim Value2 As Integer
Dim Value3 As Integer
Dim a As Integer
Dim b As Integer
Value1 = 1
Value2 = 1
Value3 = 0
a = 1
b = 1
If a > Value1 AndAlso b > (Value2 / Value3) Then
' {body of If block}
End If
MsgBox("Hey! It works!")

First, of course, we have to declare our variables with VB .NET. Then after changing And to AndAlso, the program works! One reason is that the last part of the compound If condition - (value 2 / value3) - is never actually executed. When you use AndAlso, VB .NET knows that the expression can't succeed once it is determined that the first part of the condition - a is not greater than Value1 - is false. So VB .NET stops evaluating the expression right there.

Knowledgeable .NET'ers will point out that VB .NET supports a value for infinity - which is what the result of division by zero actually is in mathematical terms - so the expression would work anyway. Very true! But consider this example where our program tests a value obtained directly from the user:

Dim Value1 As Integer
Dim a As Integer
Dim b As String
Value1 = 1
a = 1
b = 1
If a > Value1 AndAlso b = Console.ReadLine() Then
' {body of If block}
End If
MsgBox("Hey! It works!")

In VB 6 (or if you use And in VB .NET), the program will ask for input every time. If you want the second test to be executed every time, use And. If that's not what you want, use AndAlso.

This analysis suggests how you can add some efficiency to your code by arranging a compound logical expression correctly. If you place the expression that is most likely to be false in the leftmost position when using AndAlso, you can prevent execution cycles from being used to evaluate the rightmost expression. In a single test, it wouldn't make enough difference to be worth even thinking about. But if your test is inside a loop of some kind and is executed zillions of times, it could make a big difference.

The OrElse operator has similar differences compared with the VB 6 Or operator. Knowing about these two new VB .NET logical operators can help you avoid very subtle errors or achieve subtle efficiencies.

<< Globalization of... / 如何使用SetInterval >>

专题推荐

不平凡的水果世界

不平凡的水果世界

平凡的水果世界,平凡中的不平凡。 今朝看水果是水果 ,看水果还是水果 ,看水果已不是水果。这境界,谁人可比?在不平凡的水果世界里,仁者见仁,智者见智。

中国春节的那些习俗

中国春节的那些习俗

正月是农历新年的开始,人们往往将它看作是新的一年年运好坏的兆示期。所以,过年的时候“禁忌”特别多。当然,各个地方的风俗习惯不一样,过年的禁忌也是不一样的。

评论
0/200
表情 验证码:

gisman

  • 文章总数0
  • 画报总数0
  • 画报点击数0
  • 文章点击数0
个人排行
        博文分类
        日期归档