我想我很清楚两者之间的区别ByVal
and ByRef
在 VB 中,但我的问题是当我尝试将它与声明为的成员结合使用时WithEvents
.
我有以下方法:
Private Sub SafeCloseAndDeRefConnection(ByRef cnx As ADODB.Connection)
On Error GoTo ErrH
If Not cnx Is Nothing Then
If (cnx.State And adStateConnecting) = adStateConnecting Then
cnx.Cancel
End If
If (cnx.State And adStateOpen) = adStateOpen Then
cnx.Close
End If
Set cnx = Nothing
End If
Exit Sub
ErrH:
Set cnx = Nothing
End Sub
如果我有一个这样声明的类成员:
Private WithEvents Connection As ADODB.Connection
然后我想关闭连接,然后这样调用它:
SafeCloseAndDeRefConnection Connection
但在拨打电话后SafeCloseAndDeRefConnection
the Connection
变量是not set to Nothing
并且仍然有其原始参考。
如果我删除WithEvents
关键字调用SafeCloseAndDeRefConnection
按预期工作(但显然事件无法处理)
谁能向我解释为什么会发生这种情况?
附:我找到了类似的,但该解决方法在我的场景中不起作用。
也许可以打电话:
Set Connection = Nothing
After SafeCloseAndDeRefConnection(Connection)
这将强制销毁对象,而不是依赖 VB6 来为您完成!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)