Windows Topics All Articles Power UserSystem AdministrationVBScriptWireless

VB .NET Language in a Nutshell: What's New and Different in VB .NET
Pages: 1, 2, 3, 4, 5

### Boolean and Bitwise Operators

`Eqv` and `Imp`, two infrequently used Boolean and bitwise operators that are present in VB6, have been removed from VB .NET.

In VB6, `Eqv` is the logical equivalence operator. As a Boolean operator, it returns `True` if both expressions are either `True` or `False`, but it returns `False` if one is `True` while the other is `False`. As a bitwise operator, it returns 1 if both bits are the same (that is, if both are 1 or both are 0), but it returns 0 if they are different. In VB .NET, `Eqv` can be replaced with the equals comparison operator for logical operations. However, for bitwise operations, you'll have to resort to a bit-by-bit comparison, as the following code fragment shows:

``````Public Function BitwiseEqv(x1 As Byte, X2 As Byte) _
As Long

Dim b1, b2, bRet As Byte
Dim iCtr as Integer

For iCtr = 0 to len(x1) * 8 - 1
b1 = x1 and 2^iCtr
b2 = x2 and 2^iCtr
if b1 = b2 then bRet += 2^iCtr
next

BitwiseEqv = bRet

End Function ``````

In VB6, `Imp` is the logical implication operator. As a Boolean operator, it returns `True` unless its first expression is `True` while the second is `False`. As a bitwise operator, it returns 1 unless the bit in the first expression is 1 while the bit in the second expression is 0. In VB .NET, `Imp` can be replaced with a combination of the `Not` and `Or` operators for logical operations. For example, the code fragment:

``bResult = (Not bFlag1) Or bFlag2``

is equivalent to the VB6 statement:

``bResult = bFlag1 Imp bFlag2``

For bitwise operations, a bit-by-bit comparison is again necessary, as the following code fragment shows:

``````Public Function BitwiseImp(x1 As Byte, X2 As Byte) As Long

Dim b1, b2, bRet As Byte
Dim iCtr as Integer

For iCtr = 0 to len(x1)*8 - 1
b1 = Not(x1) and 2^iCtr
b2 = x2 and 2^iCtr
if b1 Or b2 then
bRet += 2^iCtr
end If
next

BitwiseImp = bRet

End Function``````

### Changes Related to Procedures

VB .NET features a number of changes to the way in which procedures are defined and called, most of which tend to make the language more streamlined and consistent.

#### Calling a procedure

In VB 6, parentheses are required around arguments when making function calls. When calling a subroutine, parentheses are required when using the `Call` statement and proscribed when not using the `Call` statement.

In VB .NET, parentheses are always required around a nonempty argument list in any procedure call--function or subroutine. (In subroutine calls, the `Call` statement is optional.) When calling a parameterless procedure, empty parentheses are optional.

#### Default Method of Passing Arguments

In VB 6, if the parameters to a function or subroutine were not explicitly prefaced with the ByVal or ByRef keywords, arguments were passed to that routine by reference, and modifications made to the argument in the function or subroutine were reflected in the variable's value once control returned to the calling routine. In VB .NET, on the other hand, if the ByRef or ByVal keyword is not used in a parameter, the argument is passed to the routine by value, and modifications made to the argument in the function or subroutine are discarded once control returns to the calling program.

#### Optional arguments

In VB 6, a procedure parameter can be declared as `Optional` without specifying a default value. For optional Variant parameters, the IsMissing function can be used to determine whether the parameter is present.

In VB .NET, an optional parameter must declare a default value, which is passed to the procedure if the calling program does not supply an argument for that parameter. The IsMissing function is not supported. The following example shows an optional parameter declaration:

``Sub Calculate(Optional ByVal Switch As Boolean = False)``

#### Return statement

In VB .NET, the `Return` statement is used to return control to the calling program from a function or subroutine. The `GoSub` statement is not supported. Note that the `Return` statement is used to return a value from a function.

The following function illustrates the `Return` statement:

``````Public Function Test(  ) As Integer
If MsgBox("Return", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Return 0
Else
MsgBox("Continue")
Return 1
End If
End Function``````

#### Passing property parameters in procedures

Consider passing a property to a procedure by reference, as in:

``````Sub ShrinkByHalf(ByRef lSize As Long)
lSize = CLng(lSize/2)
End Sub

Call ShrinkByHalf(Text1.Height)``````

In VB 6, when passing the value of a property by reference, the property is not updated. In other words, passing a property by reference is equivalent to passing it by value. Hence, in the previous example, the property Text1.Height will not be changed.

In VB .NET, passing a property by reference does update the property, so in this case, the Text1.Height property will be changed. Note, however, that the value of the property is not changed immediately, but rather when the called procedure returns.

#### ParamArray parameters

In VB 6, if the `ParamArray` keyword is used on the last parameter of a procedure declaration, the parameter can accept an array of Variant parameters. In addition, `ParamAarray` parameters are always passed by reference.

In VB .NET, `ParamArray` parameters are always passed by value, and the parameters in the array may be of any data type.

 Pages: 1, 2, 3, 4, 5