Passing Parameters

Usually the code in a procedure needs some information about the state of the program to do its job. This information is passed to the procedure as arguments when the procedure is called.

Argument Data Types

The arguments for procedures you write have the Variant data type by default. However, you can declare other data types for arguments. For example, the following function accepts a string and an integer:

Function Reverse (S As String, ByVal n As Integer)

' Reverses the first n characters in S.

Dim Temp As String, i As Integer

If n > Len(S) Then n = Len(S)

For i = n To 1 Step -1

Temp = Temp & Mid(S, i, 1)

Next

Reverse = Temp & Right(S, Len(S) - n)

End Function

Passing Arguments By Value

Only a copy of a variable is passed when a variable is passed by value. If the procedure changes the value, the change affects only the copy and not the variable itself. You must use the ByVal keyword to indicate a variable is passed by value.

For example:

Sub PostAccounts(ByVal intAcctNum as Integer)

.

. ' Place statements here.

.

End Sub

Arguments By Reference

Passing arguments by reference gives the procedure access to the actual variable contents in its memory address location. As a result, the variable’s value can be permanently changed by the procedure to which it is passed. Passing by reference is the default in Visual Basic.

If you specify a data type for an argument passed by reference, you must pass a value of that type for the argument. You can work around this by passing an expression, rather than a data type, for an argument. Visual Basic evaluates an expression and passes it as the required type if it can. The simplest way to turn a variable into an expression is to enclose it in parentheses. Using the Reverse function from the example in “Argument Data Types,” the following code turns a variable into an expression by enclosing it in parentheses:

Debug. Print Reverse ((V), 4) ' Turns the variable into an expression.

Optional Arguments

You can specify that arguments to a procedure are optional by placing the Optional keyword in the argument list. If you specify an optional argument, all subsequent arguments in the argument list must also be optional and declared with the Optional keyword. The optional arguments must be of the Variant data type. The two pieces of sample code below assumes there is a Form with a Command button and List box.

For example, this code provides all optional arguments:

Dim strName As String

Dim varAddress As Variant

Sub ListText(Optional x As String, Optional y As Variant)

List1.AddItem x

List1.AddItem y

End Sub

Private Sub Command1_Click()

strName = "yourname"

varAddress = 12345 ' Both arguments are provided.

Call ListText(strName, varAddress)

End Sub

This code, however, does not provide all optional arguments:

Dim strName As String

Dim varAddress As Variant

Sub ListText(x As String, Optional y As Variant)

List1.AddItem x

If Not IsMissing(y) Then

List1.AddItem y

End If

End Sub

Private Sub Command1_Click()

strName = "yourname" ' Second argument is not provided.

Call ListText(strName)

End Sub

Using an Indefinite Number of Arguments

Generally, the number of arguments in the procedure call must be the same as in the procedure specification. Using the ParamArray keyword allows you to specify that a procedure will accept an arbitrary number of arguments. This allows you to write functions like Sum:

Dim x As Variant

Dim y As Integer

Dim intSum As Integer

Sub Sum(ParamArray intNums())

For Each x In intNums

y = y + x

Next x

intSum = y

End Sub

Private Sub Command1_Click()

Sum 1, 3, 5, 7, 8

List1.AddItem intSum

End Sub

Creating Simpler Statements with Named Arguments

For many built-in functions, statements, and methods, Visual Basic provides the option of using named arguments as a shortcut for typing many argument values. With named arguments, you can provide any or all of the arguments, in any order, by assigning a value to the named argument. You do this by typing a colon followed by an equal sign (:=) and placing that assignment in any sequence delimited by the list separator character. You must notice that the arguments in the following example are in the reverse order of the actual syntax:

Set CurDB = OpenDatabase(exclusive:= False, source:= "Fox 2.5", _ readonly:= False, dbname:="C:\FoxData")

You can use named arguments with the functions and procedures you create, too. Visual Basic automatically associates argument names with their corresponding procedures.

Function ListText(strName As String, Optional varAddress As Variant)

List1.AddItem varAddress

List1.AddItem strName

End Sub

Private Sub Command1_Click()

ListText varAddress:=12345, strName:="Your Name"

End Sub

This is especially useful if your procedures have several optional arguments that you do not always need to specify.

Objects as Parameters

You have seen how to pass numeric values,strings, arrays as parameters . Visual Basic also allows you to pass Controls and Forms as parameters .

The syntax of passing objects as parameters to the procedures is similar to passing numeric values and variables . In the procedure declaration you should simply use the keywords Control or Form in place of Integers,Strings, and so on. For example :

Sub Check ( Buttn As Control )

Buttn.FontItalic=True

End Sub

In the calling routine , you pass the object as a parameter by using the objects name. You could pass a button named Butnext to the above procedure in the following manner :

Check Butnext

In a similar manner, you can declare a parameter to be a Form. You can set and examine the Form properties . You can pass the Form by name , or You can use the built-in variable Me , Which stands for the currently active Form.

No comments:

Post a Comment