Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AVEVA™ Plant SCADA

Passing Variables Byref and Byval

  • Last UpdatedJul 18, 2023
  • 3 minute read

Passing an argument by reference (using the Byref parameter) passes a pointer to the memory location of that argument. A pointer is just a memory address that indicates where the value is stored. If the procedure modifies that argument's value, it modifies the source of that argument, so when execution returns to the calling procedure, the source contains the modified value.

Passing an argument to a function by value (using the Byval parameter), on the other hand, passes a copy of the value as the argument. This prevents that function from modifying the source of the argument. When execution returns to the calling procedure, the source contains the same value it did before the function was called.

The Byref parameter is the default in VBA and does not need to be used explicitly within VBA. Byref gives other subroutines and functions permission to make changes to the source of the values that are passed in Byref. The keyword Byval denies this permission so the argument source cannot be altered.

There are two possible methods for indicating to VBA that you wish to pass an argument by value:

  • When declaring the argument in the subroutine or function declaration statement, by using the Byval keyword placed immediately before the argument name. This forces the subroutine or function to use a copy of the argument passed in and not modify the source. For example, the following function TestPassArg has declared its first argument intVal as being requested Byval.

    Function TestPassArg(ByvalintVal As Integer, varVal, strVal as String)

  • When passing an argument to a subroutine or function, by enclosing the individual argument within parentheses. Only the value of the argument, and not its address in memory, is passed to the subroutine or function, so that the source of the argument is not modified. For example, only the variable var3 is passed by value to the subroutine TestPassArg (because only that argument is enclosed within parentheses in the subroutine call).

    TestPassArg var1, var2,(var3)

  • In the next example, the parameter iVar is passed by value to the function TestFunction. Since arguments passed to functions must be enclosed in parentheses, an extra pair is used to force the argument to be passed by value.

    TestFunction((iVar))

    Note:
    • Plant SCADA does not support passing by reference, so Plant SCADA tag values need to be declared by value when passed as arguments to a VBA procedure from within a Plant SCADA command or expression field. This is best done by declaring the variable, assigning it the tag value, then passing the variable by value. (See the example below.)
    • Passing arrays to functions is not supported.

Example

Suppose you had a variable tag of integer type named "iTag1" and you need to pass it to a function. From within a VBA script, or Plant SCADA command or expression field, you would use the following code example to pass the variable tag value to a function named TagArgumentTest:

CiVBA
Dim iVar1 as Integer
iVar1 = iTag1
TagArgumentTest(iVar1)

Note: Cicode does not support passing by reference, so VBA variables passed to Cicode functions using the CicodeCallOpen function must be enclosed in brackets to force the passing of those variables by value.

See Also

Passing Arguments to DLL Functions from VBA

DLLs and APIs

Arguments

In This Topic
TitleResults for “How to create a CRG?”Also Available in