Changes to Procedure Declarations


Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Appendix A.  VB6 Programming Element Changes in VB .NET

Changes to Procedure Declarations

Many procedures are no longer supported in Visual Basic .NET. The fact that Visual Basic .NET is strongly typed makes the procedures or the constructs irrelevant; thus irrelevant procedures were removed or retasked in Visual Basic .NET.

IsMissing Is Missing

The IsMissing function checks for Optional Variant parameters that may not have a value. VB6 allowed Optional arguments to be declared without a default value.


Many of the constructs that have changed or been removed are demonstrated in the VB6 sample code, Grammar.vbp, on this book's Web site at

 Private Sub HasOptional(Optional ByVal Value As Variant)   MsgBox IsMissing(Value) End Sub 

If the caller invokes the HasOptional procedure without a value, the missing flag in the Variant type is set to True; in VB6 the IsMissing function could be used to test for an uninitialized Variant parameter. (IsMissing does not work on native data types.)

IsMissing is counterintuitive to the purpose of Optional parameters. An Optional param-eter is defined when there is a value that supports the general case of the procedure. In such a general case, there is a default value that makes sense. If there is no default value, do not use an Optional parameter. Visual Basic .NET requires that all Optional parameters have a default value. The default value is appended as a suffix to the end of the parameter, as in: Private Sub HasOptional(Optional ByVal MyParam As Boolean = True). IsMissing is not supported in Visual Basic .NET.

Providing a default value tells consumers what the general case is and allows you to get rid of code checking to see if there is a valid value for the parameters. Parameters should always have a valid value. A good technique is to use the Debug.Assert method to ensure that every consumer passes a valid value for each parameter.

Procedure Invocation

Use parentheses in Visual Basic .NET. If you type a procedure with an empty parameters list in Visual Basic .NET, the IDE will insert the empty parameter list represented by empty parentheses for you. You can precede procedure calls with the Call keyword, but it is no longer required. An empty procedure call looks like the following:


The Return statement has been retasked and has a new meaning. Read the next subsection for revisions to the use of the Return keyword.

Using Return

The Return keyword was used to return from a GoSub branch statement. GoSub is not supported in Visual Basic .NET. (See the section on "Changes to Flow" control for more on GoSub.) The Return keyword has been retasked; Visual Basic .NET uses Return to immediately return from a subroutine or function.

When used with a subroutine, Return requires no modifiers. By default, when execution reaches the End Sub line, the code returns to the calling procedure. You can, however, use Return to exit from a subroutine. When used in this manner, Return behaves exactly like the Exit Sub statement, branching immediately out of the current procedure.

When used with a function, the Return statement must return a value of the type specific in the As datatype clause in the function statement:

 Function CalledFunction() As Integer   CalledFunction = 43 End Function Function CalledFunction() As Integer   Return 43 End Function 

The fragment shows the older and newer styles of returning a value from a function. Visual Basic .NET still supports assigning the return value from a function to the function name , but the Return statement is the preferred method and the older style of returning values from functions is likely to disappear in future versions of Visual Basic.

Passing Properties by Reference

If you pass a Property by reference (using the ByRef modifier), VB6 does not reflect the modified property value when the procedure returns. In essence the ByRef Property behaves like a value argument.

 Private Sub ByRefProperty(ByRef AHeight As Integer)   AHeight = 10 End Sub Private Sub Command5_Click()   Call ByRefProperty(Me.Height) End Sub 

A reader might presume that the height of the object represented by Me (a form in the example) is modified and has a new value of 10 when the procedure ByRefProperty returns. In VB6 the code had no effect on the height of the form. In Visual Basic .NET the equivalent code is actually modifying the height property of the form. Here is the equivalent code in Visual Basic .NET:

 Private Sub ByRefProperty(ByRef AHeight As Integer)   AHeight = 10 End Sub Private Sub Form1_Load(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles MyBase.Load   ByRefProperty(Me.Height) End Sub 

After the equivalent Visual Basic .NET code runs, the object represented by Me has been resized to a Height of 10 twips. Be careful when you are passing properties; if you want the change to a property reflected back to the consumer, pass the property ByRef. If you want the property changes to be local to the called procedure, define the parameter as a ByVal parameter.

Argument Modifiers Default to ByVal

Visual Basic 6 defaults argument modifiers to ByRef. Visual Basic .NET defaults to ByVal. If you do not explicitly type a modifier for an argument, the IDE will add ByVal for you.

It is a good practice to always explicitly indicate your intent; this rule applies to argument modifiers, too.

ParamArray Arguments Are Always Passed ByVal

Arguments defined using the ParamArray modifier are always passed ByVal in Visual Basic .NET. Additionally, all of the arguments stored in the parameter array must be of the type declared in the As clause of the parameter array declaration.

VB6 allowed you to declare Variant type parameter arrays. You cannot use the modifiers ByVal, ByRef, or Optional with ParamArray arguments. In VB6, ParamArray arguments were always implicitly optional and passed ByRef. The following code demonstrates the ParamArray in VB6 and why the behavior is risky.

 Private Sub HasParamArray(ParamArray Data() As Variant)   Dim I As Integer   For I = LBound(Data) To UBound(Data) 3    Debug.Print Data(I)     Data(I) = "Changed"   Next I End Sub Private Sub Command6_Click()   Dim A, B, C As Variant   A = 1   B = "B"   C = Now   Call HasParamArray(A, B, C)   Call HasParamArray(A, B, C) End Sub 

In the fragment, HasParamArray is called with three different kinds of arguments: an integer, a string, and a date and time value, a double. HasParamArray displays each value in the parameter array data and then modifies the element. When HasParamArray is called a second time, each value of the array is "Changed". Code like the preceding is difficult to debug and maintain because you would probably have to write a lot of error-handling and runtime type-checking code to determine what to do with the elements of the ParamArray. In Visual Basic .NET, the elements of the ParamArray are immutable from the caller's perspective, and every element is the same data type.

Using the Static Modifier

Static variables maintain their value between successive calls to the procedure. Visual Basic 6 allows you to add the Static modifier in the procedure definition, for example, Private Static Sub ProcName(). When used in this manner, the Static modifier caused every local variable in the procedure to be static.

 Private Static Sub HasStatic()   Dim I As Integer   I = I + 1   Dim S As String   S = S & I   Debug.Print I & " " & S End Sub 

Each successive call to HasStatic increments I and concatenates the value of I to the string S. After the first call, the Immediate Window contains 1 1; the output to the Immediate Window progresses to 2 12, 3 123, 4 1234, 5 12345, and so on.

Visual Basic .NET does not support the Static modifier in the procedure heading that begins a subroutine or function block. Visual Basic .NET does support static local variables, but you must use the Static keyword explicitly for every local variable you want to be static. Identical code in Visual Basic .NET would look like the following procedure.

 Private Sub HasStatic()   Static I As Integer   I += 1   Static S As String   S &= I   Debug.WriteLine(String.Format("{0}  {1} ", I, S)) End Sub 

In the revised listing for Visual Basic .NET, both I and S are defined as Static variables. The local variable I is incremented with the new increment operator (+=), and I is appended to the string S using the concatenate operator (&=). Debug.WriteLine displays the result in the Output window using the class method String.Format to format the output.


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
Year: 2001
Pages: 222 © 2008-2017.
If you may any questions please contact us: