简体   繁体   English

VBScript:将对象传递给函数?

[英]VBScript: Passing Object to Function?

The below example generates an error: 下面的示例生成一个错误:

VBScript compilation error: Cannot use parentheses when calling a Sub VBScript编译错误:调用Sub时不能使用括号

This error does not occur if all parameters are not objects. 如果所有参数都不是对象,则不会发生此错误。

Is there a special way to pass object parameters to VBScript functions? 是否有将对象参数传递给VBScript函数的特殊方法?

Option Explicit

Dim obj

Function TestFunc(obj)
 WScript.Echo "Why doesn't this work?"
End Function

Set obj = CreateObject("Scripting.Dictionary")
obj.Add("key", "val")

TestFunc(obj) ' Error here!

Only use parenthesis when you get a return value. 仅在获得返回值时才使用括号。 Also use Sub instead of Function for functions that don't return something. 对于不返回内容的函数,也可以使用Sub而不是Function。

Parenthesis can only be used when.. 只有在以下情况下才能使用括号:

  1. You are calling a function that returns a value. 您正在调用一个返回值的函数。
  2. When the sub/function you are calling only takes 1 parameter. 当您要调用的子功能仅带1个参数时。
  3. If you precede the call with the Call keyword eg. 如果您在通话之前使用Call关键字,例如。 Call TestFunc(obj)

This line 这条线

obj.Add("key", "val")

is causing the issue 导致问题

change to 改成

obj.Add "key", "val"

obj.Add is a sub which like the error says you can't use parens but your TestFunc is a function and can be called with parens obj.Add是一个子类,类似于错误,它说您不能使用parens,但是您的TestFunc是一个函数,可以使用parens进行调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM