vba - VBA - 我的函数需要一个参数 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)
获得返回值后发生的事情是 txtPath 也发生了变化。但是,如果我将其修改为
AvailableDirPath(ByVal strBase as String) As String
然后,txtPath 没有改变。
我认为默认参数采用 ByVal,但为什么它采用 ByRef?
解决方案
正如@Mark 提到的,默认值为 ByRef。因此,如果您不打算修改参数,则需要在参数中显式定义 ByVal。
这是MS解释的链接。
推荐阅读
- pandas - 如何将两个熊猫数据框加入/写入单个文件
- javascript - 我制作了一个全屏表格,但它干扰了导航栏
- java - 获取特定的 InflatedView Android Studio
- node.js - 在 node.js 中使用 setInterval 和 promise 调用 rest api
- c - 将 %s 用于 char 数组变量时出现 printf 问题。获取警告信息
- javascript - 使用 javascript on html 按钮将 json 数据发送到托管的 python flask api,然后在 HTML 标签上打印来自 API 的响应
- c# - 删除操作将 null 设置为导航属性
- c# - 从一个 notifyIcon 菜单控制所有运行的应用程序
- sql - SQL 多个日期范围
- python - 将 flask.request 传递给另一个线程