VBA - 我的函数接受参数 ByRef 而不是 ByVal

VBA - my function takes an arument ByRef instaed of ByVal

我遇到了一个奇怪的情况。我在 Windows 10.

上使用 MS Access Office 365

调用者的一个参数被改变了。这意味着 strBase 被视为 ByRef.

Public Function AvailableDirPath(strBase As String) As String
' Find non existing folder path.
'   strBase : folder path that a folder is created.
'   Return: Full system path for the directory

    Dim bFound As Boolean: bFound = Falase
    
    Do Until FolderExists(strBase) = False
        ' The ParentFolderPath returns full path of parent folder. 
        strBase = ParentFolderPath(strBase) & "\" & (Int(99999 * Rnd) + 10000)
    Loop
    

    AvailableDirPath = strBase

End Function

来电方就在下方。

Dim txtPath As String: txtPath = "C:\windows\temp\MSACCESS_TEST\"
Dim strRes as String
strRes = modFileFolder.AvailableDirPath(txtPath)

获取 return 值后会发生什么 txtPath 也已更改。但是如果我将它修改为

AvailableDirPath(ByVal strBase as String) As String

然后,txtPath 没有改变。

我认为默认参数采用 ByVal 但为什么它采用 ByRef?

如@Mark 所述,默认值为 ByRef。因此,如果您不打算修改参数,则需要在参数中明确定义 ByVal。

这里是linkMS的解释

https://docs.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/array-argument-must-be-byref