Vb. NET programming introduction of the second (transformation)

Source: Internet
Author: User
Tags bitwise constant integer
Programmatic use of And/or/not operations on Boolean types of data
The AND and or keywords are made in Visual Basic.NET and Visual Basic 6.0
Used differently, in Visual Basic 6.0, the and operation performs both a logical and a bitwise AND operation, depending on the operand (if it is a Boolean type, performs a logical AND operation, and, if it is a number, performs a bitwise AND operation). In Visual Basic.NET, the and operation performs only logical and operations. For a bitwise operation, a set of actions is provided in Visual Basic.NET: Bitand, Bitor, Bitnot, and Bitxor.

Here are the sample programs that demonstrate these differences

Dim A as Integer
Dim B as Integer
Dim C as Boolean
A = 1
b = 2
c = A and b
MsgBox ("The answer is" & c)

When the above code runs under Visual Basic 6.0, the result is false (execution
Bitwise AND operations), but under Visual Basic.NET, the result is true (the logical AND operation is performed).
To make sure your program is running after the upgrade as well as before the upgrade, Visual
Basic.NET contains compatible functions Vb6.and, VB6. Or and Vb6.not, the results of these functions are the same as those performed by the And/or/not operation under Visual Basic 6.0. When the code is upgraded, the result looks like this:

Dim A as Short
Dim B as Short
Dim C as Boolean
A = 1
b = 2
c = VB6. and (A, B)
MsgBox ("The answer is" & c)

The upgraded code will result in false results, as it would run under Visual Basic 6.0

If you don't want your code to be upgraded to use a compatible function, you need to make sure that your and/or/not is all used in Boolean-type data operations, for example, if the above code changes to the following way:

Dim A as Integer
Dim B as Integer
Dim C as Boolean
A = 1
b = 2
c = a <> 0 and b <> 0
MsgBox ("The answer is" & c)

When the upgrade, the code is almost the same as the original:

Dim A as Short
Dim B as Short
Dim C as Boolean
A = 1
b = 2
c = a <> 0 and b <> 0
MsgBox ("The answer is" & c)

Unlike the previous one, each operation is performed between Boolean data, because
These are all logical and operations used in Visual Basic 6.0. So no changes will occur after you upgrade to Visual Basic.NET. The point is that you can copy and paste the code directly between Visual Basic.NET and Visual Basic 6.0, and your code will run faster because it uses a local and operation instead of a compatible function.

Another issue to consider is the and/or/not operation of the function result.
Questions, such as the following example:

Dim B as Boolean
b = Function1 () and Function2 ()

In Visual Basic 6.0, the results of both Function1 and Function2 are executed
And then the results are then manipulated. In Visual Basic.NET, Function2 is evaluated only if the Function1 result is true. Because the and operation in Visual Basic.NET performs a logical AND operation, if the Function1 result is false,and The result is definitely false, the Function2 is no longer executed to speed up. But if
Function2 for other purposes, such as to avoid certain errors, there is an unforeseen
Know the error, then the above code will be upgraded to look like the following:

Dim B as Boolean
b = VB6. and (Function1 (), Function2 ())

In order not to have a compatible function in the upgrade code, the original code needs to make the following changes:

Dim B as Boolean
Dim C as Boolean
Dim D as Boolean
c = Function1 ()
D = Function2 ()
B = C and D

Another note is that in Visual Basic.NET, the true value will no longer be-1
But 1. This change is made to enable Visual Basic.NET to integrate into the. NET language. Because of this change, you need to use constant true instead of-one in your Visual Basic 6.0 project to represent truth values, and to save Boolean data using a Boolean type instead of an integer type variable. The following code illustrates the Visual Basic 6.0 and Visual Basic.NET
The difference between:

Dim I as Integer
i = True
If i =-1 Then
MsgBox ("True")
Else
MsgBox ("False")
End If
In Visual Basic 6.0, the result is true, whereas in Visual Basic.NET the result
to False.

If you make the following changes to your code, the program will display true if it runs in both languages

Dim I as Boolean
i = True
If i = True Then
MsgBox ("True")
Else
MsgBox ("False")
End If

You can see from the example above that you need to be aware of two points in your program:
1, use constant true and false to determine whether and not to use 0 and-1
2. Use Boolean variables to store Boolean values instead of integer type
If you don't do this in your program, you'll need to do a lot of manual changes after the upgrade.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.