vb6 – 为什么ByRef不能与WithEvents一起使用?
发布时间:2020-12-17 07:27:13 所属栏目:百科 来源:网络整理
导读:我想我很清楚VB中ByVal和ByRef之间的区别是什么,但我的问题是当我尝试将它与使用WithEvents声明的成员结合使用时. 我有以下方法: Private Sub SafeCloseAndDeRefConnection(ByRef cnx As ADODB.Connection)On Error GoTo ErrH If Not cnx Is Nothing Then I
我想我很清楚VB中ByVal和ByRef之间的区别是什么,但我的问题是当我尝试将它与使用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之后,Connection变量未设置为Nothing且仍具有其原始引用. 如果我删除WithEvents关键字,对SafeCloseAndDeRefConnection的调用按预期工作(但显然不能处理事件) 任何人都可以向我解释为什么会这样吗? 附:我找到了类似的question elsewhere,但解决方法在我的方案中不起作用. 解决方法
也许打电话:
Set Connection = Nothing SafeCloseAndDeRefConnection(连接)之后 这将强制破坏对象,而不是依靠VB6为你做! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |