Vb6 byref



Keywords: vb6 byref
Description: Many, if not most, procedures accept input parameters that are used to calculate the result of a function or otherwise direct the action of a procedure. These parameters may be passed By Reference

Many, if not most, procedures accept input parameters that are used to calculate the result of a function or otherwise direct the action of a procedure. These parameters may be passed By Reference (ByRef) or By Value (ByVal).

TERMINOLOGY NOTE: Strictly speaking, the term argument refers to passed values from the perspective of the passing-from procedure. For example, the calling procedure CallingSub passes arguments Arg1 and Arg2 to the procedure CalledSub. The term parameter refers to values passed into a called procedure, from the perspective of the called procedure. For example, the procedure CalledSub accepts parameters X and Y from CallingSub. However, these terms are used interchangably in most documentation and will be used interchangably on this site. The meaning will be clear from the context provided by the narrative text.

Passing ByRef or ByVal indicates whether the actual value of an argument is passed to the CalledProcedure by the CallingProcedure. or whether a reference (called a pointer in some other languages) is passed to to the CalledProcedure. If an argument is passed ByRef. the memory address of the argument is passed to the CalledProcedure and any modification to that parameter by the CalledProcedure is made to the value in the CallingProcedure. If an argument is passed ByVal. the actual value, not a reference to the variable, is passed to the CalledProcedure.

In the CallingProcedure the variables A and B are assigned the values 123 and 456, respectively. These values are confirmed with the first Debug.Print statement. Then, the CalledProcedure is called passing arguments A and B. Within CalledProcedure the parameters X and Y are assigned the values 321 and 654 respectively, and control is returned back to the procedure CallingProcedure. Since the parameter X was declared with ByRef. a reference or pointer to A was passed to CalledProcedure and any modification to the X parameter in CalledProcedure affects the variable A in CallingProcedure. The parameter Y was declared with ByVal. so only the actual value of B was passed to CalledProcedure. Changes made to the parameter Y are not made to the variable B. This is illustrated by the second Debug.Print statement. This shows that A was modified by CalledProcedure but that B was not changed.

Objects are always passed by reference. The ByRef and ByVal modifers indicate how the reference is passed to the called procedure. When you pass an object type variable to a procedure, the reference or address to the object is passed -- you never really pass the object itself. When you pass an object ByRef. the reference is passed by reference and the called procedure can change the object to which that reference refers to. When an object is passed ByVal an copy of the reference (address) of the object is passed.

In the CallingProcedure, the variable Range1 is set to Range("A1") and the variable Range2 is set to Range("A2"). Then CalledProcedure is called, passing ByRef R1 and ByVal R2. The CalledProcedure sets the values of these ranges to new values and then changes the ranges to which R1 and R2 refer. Since R1 was passed ByRef. the CalledProcedure can change the cell to which Range1 in CallingProcedure refers to. As is confirmed by the second group of Debug.Print statements, the variable Range1 now refers to (points to) Range("A3"). not Range("A1"). However, since R2 was passed ByVal. the CalledProcedure cannot change the range to which Range2 refers to in CallingProcedure.

Throughout this article, we have discussed that you cannot change the value of a variable in the calling procedure if a parameter referring to that variable was passed ByVal. It is worth noting, for clarity, though, that you can certainly change the value of a parameter variable regardless of whether is was passed by ByRef or ByVal. These declarations refer to only if the variable in the calling procedure can be changed. You can always change the value of a parameter in the called procedure. For example, In this code, the CalledProcedure is free to change the values of its parameters X and Y. and indeed this is often quite useful. The ByRef and ByVal declaration affect only whether changes to parameters affect the variables in the CallingProcedure. This same rule applies to object type parameters: This code clearly illustrates that the object parameters R1 and R2 can be changed within the scope of the CalledProcedure procedure. ByRef and ByVal determine only if the changes to the parameters make their way back to the calling procedure.

Even if a called procedure has declared its parameters as ByRef. you can force those to be ByVal by enclosing each argument within parentheses. First, examine the following code. It should look quite familiar as we have used it before:

The CalledProcedure changes the values of its parameters to 321 and 654 respectively. However, imagine the situation in which you do want to call on the functionality of CalledProcedure but you don't want that procedure to modify the input parameters. The difficult way would be to save the original copies of A and B and restore the values after CalledProcedure returns, or to even rewrite the CalledProcedures procedure altogether. However, VB/VBA gives you a much simpler method: just enclose the arguments individually in parentheses. For example, Here, the second Debug.Print statement illustrates that the values of A and B have not had their values changed. However, there are a few caveats to using this approach:
  • You cannot use named arguments in the call to the CalledFunction .
  • You (generally) cannot pass Object type parameters in this manner.
  • In the circumstances in which you can use object type variable (when the object has a default property and the parameter as declared as Variants), you may very well get incorrect results or a run time error because the CalledProcedure is designed to work with object not simple type properties of objects.

By default, all parameters are passed by reference, so it is not necessary to include the ByRef declaration. However, I have over my 20 years as a professional programmer found that it is good practice to include the ByRef declaration if you are going to change the value of the parameter. It makes no difference to how the code runs -- in makes the code neither faster nor slower -- but it serves as documentation that the variable in the calling procedure is going to be modified by the called procedure. It helps make the code self-documenting. You may or may not agree. Use it or don't -- whatever style you prefer. It is worth noting that in VBNET, the default method of passing variables is ByVal. since everything in VBNET is a object. This may be of importance if you are porting some VBA code to a VBNET-based document, component, or add-in.

Arrays are always passed by reference. You will receive a compiler error if you attempt to pass an array by value. See Passing And Returning Arrays With Functions for details about passing and returning array for VBA procedures.




Photogallery Vb6 byref:


Navigating Your Way Through Visual Basic 6.0 to Visual Basic.NET Appl


Presentazione VB.NET Overview Corrado Cavalli Microsoft.NET MVP ...


Visual Basic.Net ByRef vs ByVal - YouTube


Formatting/adding a continuous border around a vb6 datareport ...


Navigating Your Way Through Visual Basic 6.0 to Visual Basic.NET Appl


Whitepapers - Comparing VB Migration Partner with Upgrade Wizard


Navigating Your Way Through Visual Basic 6.0 to Visual Basic.NET Appl


Navigating Your Way Through Visual Basic 6.0 to Visual Basic.NET Appl


Navigating Your Way Through Visual Basic 6.0 to Visual Basic.NET Appl


Navigating Your Way Through Visual Basic 6.0 to Visual Basic.NET Appl


Navigating Your Way Through Visual Basic 6.0 to Visual Basic.NET Appl


Navigating Your Way Through Visual Basic 6.0 to Visual Basic.NET Appl


Navigating Your Way Through Visual Basic 6.0 to Visual Basic.NET Appl


Navigating Your Way Through Visual Basic 6.0 to Visual Basic.NET Appl


Funciones subrutinas y procedimientos


Navigating Your Way Through Visual Basic 6.0 to Visual Basic.NET Appl


Funciones subrutinas y procedimientos


Navigating Your Way Through Visual Basic 6.0 to Visual Basic.NET Appl


Presentazione Ing. Stefano Cadei -- Java1 Visual Basic Introduzione.


introduccion-a-vb6-4-638.jpg? ...