在创建对象的新实例之前将对象设置为 Nothing?

Set Object to Nothing before creating a new instance of object?

在 VBA 中,在声明对象的新实例之前将其设置为 Nothing 是一种好习惯吗?

例如:

Private Sub SetupConn()

Const Provider As String = "sqloledb"
Const LanDataSource As String = "127.0.0.1"
Const WanDataSource As String = "mail.12345678.biz:12345"

Dim UserName As String
Dim Password As String

UserName = "myusername"
Password = "mypassword"

这里是我想知道是否应该关闭 连接或在重新打开新连接之前将其设置为空。

Set mDBconn = New ADODB.Connection

Select Case mConnMethod
Case WAN
    mConnStr = "Provider='" & Provider & ";Data Source=" & WanDataSource _
             & ";User ID=" & UserName & ";Password=" & Password
Case Lan
    mConnStr = "Provider='" & Provider & ";Data Source=" & LanDataSource _
             & ";User ID=" & UserName & ";Password=" & Password
End Select
End Sub

场景是用户在路上,使用移动数据连接,他到达办公室后想直接连接到高速wifi 连接,而无需关闭并重新打开程序。在这种情况下,我要做的是将连接方法设置为 LAN 并使用重置参数调用此 sub。

据我推测Whats the difference between rs.close vs rs = nothing in a RecordSet, 最好既关闭又不设置任何内容。这回答了我的问题。