Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:scriptsamples:parentheses [2015/09/14]
127.0.0.1 external edit
developer:scriptsamples:parentheses [2015/10/15] (current)
sandy
Line 4: Line 4:
  
 =====Question===== =====Question=====
-Every now and then, I get the error message ​"Cannot use parentheses when calling a Sub" ​when calling a function or method. But it does not happen all of the time. For example, the following code appears to work:+Every now and then, I get the error message ​//Cannot use parentheses when calling a Sub// when calling a function or method. But it does not happen all the time. For example, the following code appears to work:
  
 <code vb> <code vb>
Line 17: Line 17:
  ​MyOtherSub(MyArg1,​ MyArg2)  ​MyOtherSub(MyArg1,​ MyArg2)
 </​code>​ </​code>​
 +\\
  
 =====Answer===== =====Answer=====
Line 56: Line 57:
 3.3) If 3.1 and 3.2 do not apply, then the list must not be surrounded by parentheses. 3.3) If 3.1 and 3.2 do not apply, then the list must not be surrounded by parentheses.
  
-Finally, there is the ByRef rule: arguments are passed ByRef when possible. But, if there are extra parentheses around a variablethen the variable is passed ByVal, not ByRef.+Finally, there is the ByRef rule: arguments are passed ByRef when possible. But, if there are extra parentheses around a variablethen the variable is passed ByVal, not ByRef.
  
 From these rules, it should be clear why the statement MySub(MyArg) is legal but MyOtherSub(MyArg1,​ MyArg2) is not. The first case appears to be a subroutine call with parentheses around the argument list, but that would violate rule 3.3. Then why does this work? In fact, it is a subroutine call with no parentheses around the argument list, but parentheses around the first argument. This passes the argument by value. The second case is a clear violation of rule 3.3, and there is no way to make it legal, so an error is given. From these rules, it should be clear why the statement MySub(MyArg) is legal but MyOtherSub(MyArg1,​ MyArg2) is not. The first case appears to be a subroutine call with parentheses around the argument list, but that would violate rule 3.3. Then why does this work? In fact, it is a subroutine call with no parentheses around the argument list, but parentheses around the first argument. This passes the argument by value. The second case is a clear violation of rule 3.3, and there is no way to make it legal, so an error is given.
 +
 +\\
  
 =====Examples===== =====Examples=====
developer/scriptsamples/parentheses.txt ยท Last modified: 2015/10/15 by sandy