[英]what is the VBA for the USING statement
In csharp or vb.net we use the using
statement for reasons that we know: One can open the database and close it automatically without writing it explicitly.在 csharp 或 vb.net 中,我们使用
using
语句的原因我们知道:可以打开数据库并自动关闭它,而无需显式编写它。 I would like to do the samething in VBA
我想在
VBA
做同样的事情
c# C#
using(var db=new MyDbContext()){
//do work here
}
vb.net网络
Using s = New MyDbContext()
'--..do work here
End Using
Answering just your first question, as you've hinted, Using
is just syntactic sugar for calling Dispose()
on an object instance which implements the IDisposable
interface.正如您所暗示的那样,仅回答您的第一个问题,
Using
只是在实现IDisposable
接口的对象实例上调用Dispose()
语法糖。
It is equivalent to它相当于
Dim s as MyDbContext
Try
s = New MyDbContext()
// ...
Finally
s.Dispose()
End Try
Since VBA
doesn't support the Using
sugar, and in the absence of structured Try..Catch
exception handling, you'll need to explicitly call Dispose()
on all paths which control the lifespan of the object ( MyDbContext
in your case).由于
VBA
不支持Using
糖,并且在没有结构化Try..Catch
异常处理的情况下,您需要在控制对象生命周期的所有路径上显式调用Dispose()
(在您的情况下为MyDbContext
)。 In which case you may as well have just used .Close()
在这种情况下,您也可以使用
.Close()
There is no VBA equivalent to the Using
statement.没有与
Using
语句等效的 VBA。
The VBA developer documentation provides samples and even downloadable offline versions of the VBA documentation. VBA 开发人员文档提供了VBA 文档的示例甚至可下载的脱机版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.