PRB: Can't Set Formal Parameter When Setting Object Vars (105230)
The information in this article applies to:
- Microsoft Visual Basic Standard Edition for Windows 2.0
- Microsoft Visual Basic Standard Edition for Windows 3.0
This article was previously published under Q105230 SYMPTOMS
When trying to use the Set keyword to assign a value to a formal parameter
passed to a Visual Basic Sub or Function procedure, you may get a "Can't
Set formal parameter" error at compile or run time.
Specifically, trying to use a Set statement on an object variable that is a
formal parameter of a procedure results in this error.
CAUSE
This behavior is by design. You cannot use a procedure's formal parameter
as the destination of a Set statement.
Object variables can be parameters of a Sub or Function procedure, but if
an object variable is a parameter, its value cannot be changed inside the
called procedure.
WORKAROUND
You can use a Set statement on an array of objects that is passed as a
parameter. For example, you can modify the code shown in the "Steps to
Reproduce Behavior" section to the following to prevent the error:
Sub Form_Load ()
ReDim X(0) As Form
Call MySub1(X())
End Sub
Sub MySub1 (FormalParameterX() As Form)
Set FormalParameterX(0) = Form1
FormalParameterX(0).Caption = "Success" ' Form's caption will change.
End Sub
RESOLUTION
If you make the object variable Global instead of passing it as a
parameter, you can use Set statements inside procedures.
STATUS
This behavior is by design.
Modification Type: | Major | Last Reviewed: | 10/28/2003 |
---|
Keywords: | kbcode kbprb KB105230 |
---|
|