Microsoft KB Archive/216481

= PRB: ByRef Has No Effect on Form's Public Variables =

Article ID: 216481

Article Last Modified on 5/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q216481



SYMPTOMS
When you have a subroutine that has a ByRef parameter, the subroutine changes the values of that parameter when it returns. However, if you pass a form's public variables to this subroutine, you find that the variable you passed in has not been changed.



CAUSE
Form Modules, Class Modules, and User Control Modules are object modules. The public variables defined in these modules are actually implemented as properties, as demonstrated in the following:

Public FormPubVar as Integer

is effectively implemented as:

Private m_FormPubVar as Integer

Public Property Get FormPubVar as Integer FormPubVar = m_FormPubVar End Property

Public Property Let FormPubVar(newVal as Integer) m_FormPubVar = newVal End Property

When you pass a form's public variable into a subroutine with the syntax Form1.FormPubVar, the Property Get is called and the result is placed in a temporary variable. It is this temporary variable that is actually passed to the subroutine. When the subroutine returns, only the value stored in the temporary variable is changed. The same problem occurs when passing a String parameter ByVal.



RESOLUTION
Use a temporary variable as in the following code snippet to work around the problem:

Dim temp as Integer temp = Form1.FormPubVar ChangeVar temp ' where ChangeVar is the Sub receiving the ByRef parameter Form1.FormPubVar = temp



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Create a Visual Basic Standard EXE project. Form1 is created by default. Add a CommandButton to Form1.  Paste the following code in the General Declarations section of Form1:

Option Explicit Public FormPubVar As Integer

Private Sub Command1_Click Dim LocVar As Integer

LocVar = 10 ChangeVar LocVar MsgBox "LocVar is changed from 10 to " & LocVar

ModulePubVar = 10 ChangeVar Module1.ModulePubVar MsgBox "ModulePubVar is changed from 10 to " & ModulePubVar

FormPubVar = 10 ChangeVar FormPubVar MsgBox "FormPubVar is changed from 10 to " & FormPubVar

FormPubVar = 10 ChangeVar Me.FormPubVar MsgBox "Me.FormPubVar is changed from 10 to " & Me.FormPubVar End Sub  On the Project menu, click Add module. Click the Open button in the Add Module dialog box.  Add the following code to the General Declarations section of Module1:

Option Explicit Public ModulePubVar As Integer

Public Sub ChangeVar(ByRef v As Integer) v = v + 10 End Sub </li> Run the application and click Command1. Note the values displayed in each message box, clicking OK on each one. The last message box demonstrates that Me.FormPubVar has not been changed.</li></ol>

NOTE: When you use FormPubVar directly in the form's code window, Visual Basic passes in the internal representation of the FormPubVar instead of a temporary variable as is the case when you pass Me.FormPubVar.

Keywords: kbprb KB216481

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.