简体   繁体   English

嵌套函数是否可以在VBA中使用?

[英]Are nested functions possible in VBA?

I'm trying to clean up code by stripping parameters from a function within a private scope, like this: 我试图通过从私有范围内的函数中剥离参数来清理代码,如下所示:

Function complicatedFunction(x as Double, param1 as Double, param2 as Double)
    ...
End Function

Function mainActionHappensHere(L as Double, U as Double ...)
    Function cleaner(x)
        cleaner = complicatedFunction(x, L, U)
    End Function
    ...
    cleaner(x)                       'Many calls to this function
    ...
End Function

Is this possible? 这可能吗? Compiler complains, "Expected End Function", since I'm beginning a function before ending the outer one. 编译器抱怨“预期结束函数”,因为我在结束外部函数之前开始执行函数。 And Google is no help :( PS I can't define cleaner() outside of mainActionHappensHere(), since then the correct L and U won't get passed into it. 谷歌没有帮助:( PS我不能在mainActionHappensHere()之外定义cleaner(),因为那时正确的L和U不会被传递到它。

VB.Net can do this, but I don't believe VBA can. VB.Net可以做到这一点,但我不相信VBA可以。

Two features that might help you simplify this code in other ways are overloaded functions or optional parameters. 可能有助于您以其他方式简化此代码的两个功能是重载函数或可选参数。 Here's an example using optional parameters: 以下是使用可选参数的示例:

Function complicatedFunction(x as Double, Optional param1 as Double = L, Optional param2 as Double = U) As Object
...
End Function

complicatedFunction(x)

However, L and U must be constants for this to work. 但是,L和U必须是常量才能工作。

FWIW, and in case it turns out that you're really working with a VB.Net dialect, the VB.Net syntax looks like this: FWIW,如果事实证明你正在使用VB.Net方言,VB.Net语法如下所示:

Sub complicatedFunction(x as Double, param1 as Double, param2 as Double) 
    ...
End Sub

Function mainActionHappensHere(L as Double, U as Double ...)
    Dim cleaner As Func(Of Double, Object) = 
        Function(x) 
            Return complicatedFunction(x, L, U)
        End Function

    Dim y = cleaner(x)                       'Many calls to this function
    ...
End Function

There are no nested functions in VB, either VBA or VB6 or VB.NET. VB中没有嵌套函数,无论是VBA还是VB6或VB.NET。

Limiting the scope to VBA, your options would be: 将范围限制为VBA,您的选项将是:

  • Use GoSub , one of the oldest VB command, that is deprecated, frowned upon and has no upgrade equivalent in VB.NET: 使用GoSub ,这是最老的VB命令之一,不赞成使用,并且在VB.NET中没有升级等价物:

     Function mainActionHappensHere(L as Double, U as Double ...) Dim ResultOfCleaner As Variant ... x = 5 : GoSub cleaner 'Many calls to this function 'Use ResultOfCleaner here ... x = 42 : GoSub cleaner 'Many calls to this function 'Use ResultOfCleaner here ... Exit Function cleaner: ResultOfCleaner = complicatedFunction(x, L, U) Return End Function 
  • Manually create a closure. 手动创建一个闭包。

    Define a class that exposes L and U as fields or properties. 定义一个将LU公开为字段或属性的类。 Instantiate the class, set L and U once, then call function Cleaner , also defined in that class, that calls complicatedFunction with the stored L and U . 实例化类,设置LU一次,然后调用函数Cleaner ,在该类还定义,调用complicatedFunction与存储的LU

    Obviously this creates some overhead. 显然这会产生一些开销。

You can use this syntax 您可以使用此语法

Dim fSomeFunction As Func(Of String, Boolean) = Function(ByVal something As String) As Boolean
                                                                     Return True
                                                                 End Function

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

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