简体   繁体   English

USING 语句的 VBA 是什么

[英]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做同样的事情

  1. how to do it?怎么做?
  2. Are all VB.NET statement/keywords/ available in VBA ?所有 VB.NET 语句/关键字/都可以在 VBA 中使用吗?
  3. how to tell if a given statement is(was) known in VBA ?如何判断给定的语句在 VBA 中是否已知? Is it there a library(glosary) of all VBA statements/keyword/operators ?是否有所有 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM