Difference between revisions of "Microsoft KB Archive/105230"

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
 
Line 50: Line 50:
 
== SYMPTOMS ==
 
== 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 &quot;Can't Set formal parameter&quot; error at compile or run time.<br />
+
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.<br />
 
<br />
 
<br />
 
Specifically, trying to use a Set statement on an object variable that is a formal parameter of a procedure results in this error.
 
Specifically, trying to use a Set statement on an object variable that is a formal parameter of a procedure results in this error.
Line 68: Line 68:
 
== WORKAROUND ==
 
== 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 &quot;Steps to Reproduce Behavior&quot; section to the following to prevent the error:
+
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:
 
<pre class="codesample">Sub Form_Load ()
 
<pre class="codesample">Sub Form_Load ()
 
   ReDim X(0) As Form
 
   ReDim X(0) As Form
Line 76: Line 76:
 
Sub MySub1 (FormalParameterX() As Form)
 
Sub MySub1 (FormalParameterX() As Form)
 
   Set FormalParameterX(0) = Form1
 
   Set FormalParameterX(0) = Form1
   FormalParameterX(0).Caption = &quot;Success&quot;   ' Form's caption will change.
+
   FormalParameterX(0).Caption = "Success"   ' Form's caption will change.
 
End Sub
 
End Sub
 
                 </pre>
 
                 </pre>
Line 112: Line 112:
 
   Sub MySub1 (FormalParameterX As Form)
 
   Sub MySub1 (FormalParameterX As Form)
 
       Set FormalParameterX = Form1      ' Can't Set formal parameter error
 
       Set FormalParameterX = Form1      ' Can't Set formal parameter error
       FormalParameterX.Caption = &quot;Success&quot;   ' This is never executed.
+
       FormalParameterX.Caption = "Success"   ' This is never executed.
 
   End Sub
 
   End Sub
 
                         </pre></li>
 
                         </pre></li>
<li>Attempt to run or compile the application to generate the &quot;Can't Set formal parameter&quot; error.</li></ol>
+
<li>Attempt to run or compile the application to generate the "Can't Set formal parameter" error.</li></ol>
  
 
=== More Steps to Reproduce Behavior ===
 
=== More Steps to Reproduce Behavior ===
Line 128: Line 128:
 
   End Sub
 
   End Sub
 
                         </pre></li>
 
                         </pre></li>
<li>Press the F5 key to run the application. The error &quot;Can't Set Formal Parameter&quot; should occur immediately.</li></ol>
+
<li>Press the F5 key to run the application. The error "Can't Set Formal Parameter" should occur immediately.</li></ol>
  
 
'''NOTE''': Trying to force these objects to be passed by value by setting the ByVal keyword results in this error:
 
'''NOTE''': Trying to force these objects to be passed by value by setting the ByVal keyword results in this error:

Latest revision as of 10:01, 20 July 2020

Article ID: 105230

Article Last Modified on 10/28/2003



APPLIES TO

  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition



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.

MORE INFORMATION

Objects as parameters can be thought of as a copy of the structure that defines the object. If Set statements were allowed on these objects, this would change the value inside the routine, but upon returning from the routine the changes would be lost and the object variable would revert back to its original value.

Steps to Reproduce Behavior

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Add the following code to the Form_Load procedure:

       Sub Form_Load ()
          Dim X As Form
          Call MySub1(X)
       End Sub
    
       Sub MySub1 (FormalParameterX As Form)
          Set FormalParameterX = Form1       ' Can't Set formal parameter error
          FormalParameterX.Caption = "Success"    ' This is never executed.
       End Sub
                            
  3. Attempt to run or compile the application to generate the "Can't Set formal parameter" error.

More Steps to Reproduce Behavior

Here is another set of steps that result in the error:

  1. Start a new project in Visual Basic and add the following procedure to the application:

       Sub s (tb As Table)
          Set tb = Nothing
       End Sub
                            
  2. Press the F5 key to run the application. The error "Can't Set Formal Parameter" should occur immediately.

NOTE: Trying to force these objects to be passed by value by setting the ByVal keyword results in this error:

Expected: Integer or Long or Single or Double or Currency or String or Variant.

ByVal is allowed with the variable types listed in the error message, but it is not allowed with any other variable type.


Additional query words: 2.00 3.00

Keywords: kbprb kbcode KB105230